Call OpenCv through jni and directly call openCv for android to perform simple image transformation

Source: Internet
Author: User

Call OpenCv through jni and directly call openCv for android to perform simple image transformation

Check out that if you are prompted to install xxxx. mamager during the runtime, go to Opencvforandroid now. Unzip the package and install the corresponding manager installation package.

 

This is the running effect. If you don't need to talk about jni's call, let's see that he has changed different codes.

 

 

package com.example.opencvdemo1;import org.opencv.android.BaseLoaderCallback;import org.opencv.android.LoaderCallbackInterface;import org.opencv.android.OpenCVLoader;import org.opencv.android.Utils;import org.opencv.core.Mat;import org.opencv.imgproc.Imgproc;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import com.example.openjni.ImageProc;public class MainActivity extends Activity implements OnClickListener{private Button mBtn_type1,mBtn_type2,mBtn_type3,mBtn_type4,mBtn_type5,mBtn_type6,mBtn_type7,mBtn_src;private ImageView mIv_src;private Bitmap mDes_Bitmap;private boolean isGray = false;private BaseLoaderCallback loaderCallback = new BaseLoaderCallback(this) {public void onManagerConnected(int status) {switch (status) {case LoaderCallbackInterface.SUCCESS:System.loadLibrary("image_proc");break;case LoaderCallbackInterface.INIT_FAILED:break;case LoaderCallbackInterface.INSTALL_CANCELED:break;case LoaderCallbackInterface.MARKET_ERROR:break;case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION:break;default:super.onManagerConnected(status);break;}};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}public void init(){mBtn_type1 = (Button)findViewById(R.id.btn_type1);mBtn_type2 = (Button)findViewById(R.id.btn_type2);mBtn_type3 = (Button)findViewById(R.id.btn_type3);mBtn_type4 = (Button)findViewById(R.id.btn_type4);mBtn_type5 = (Button)findViewById(R.id.btn_type5);mBtn_type6 = (Button)findViewById(R.id.btn_type6);mBtn_type7 = (Button)findViewById(R.id.btn_type7);mBtn_src = (Button)findViewById(R.id.btn_src);mIv_src = (ImageView)findViewById(R.id.iv_img);mDes_Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);mIv_src.setImageBitmap(mDes_Bitmap);mBtn_type1.setOnClickListener(this);mBtn_type2.setOnClickListener(this);mBtn_type3.setOnClickListener(this);mBtn_type4.setOnClickListener(this);mBtn_src.setOnClickListener(this);mBtn_type5.setOnClickListener(this);mBtn_type6.setOnClickListener(this);mBtn_type7.setOnClickListener(this);}public Bitmap procSrc2GrayJni(){int w = mDes_Bitmap.getWidth();int h = mDes_Bitmap.getHeight();int [] pixels = new int [w*h];mDes_Bitmap.getPixels(pixels, 0, w, 0, 0, w, h);int[] resultInt = ImageProc.grayProc(pixels, w, h);Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);bitmap.setPixels(resultInt, 0, w, 0, 0, w, h);return bitmap; }    public Bitmap procSrc2Gray(int type){          Mat rgbMat = new Mat();          Mat grayMat = new Mat();         Bitmap grayBitmap = Bitmap.createBitmap(mDes_Bitmap.getWidth(), mDes_Bitmap.getHeight(), Config.RGB_565);          Utils.bitmapToMat(mDes_Bitmap, rgbMat);//convert original bitmap to Mat, R G B.          Imgproc.cvtColor(rgbMat, grayMat,type);//rgbMat to gray grayMat          Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap          Log.i("TAG", "procSrc2Gray sucess...");          return grayBitmap;    }  @Overrideprotected void onResume() {super.onResume();OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,this, loaderCallback);}@Overridepublic void onClick(View arg0) {switch (arg0.getId()) {case R.id.btn_type1:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2GRAY)); break;case R.id.btn_type2:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2HLS));break;case R.id.btn_type3:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2HSV));break;case R.id.btn_type4:Log.e("TAG", "procSrc2Gray sucess...");  mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2Lab));break;case R.id.btn_type5:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2Luv));break;case R.id.btn_type6:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2XYZ));break;case R.id.btn_type7:mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2YUV));break;case R.id.btn_src:mIv_src.setImageBitmap(mDes_Bitmap);break;default:break;}}}
Android openCv Imgproc: this class is used to process images. You can change an image to the gray, sketch, or negative type you want, yes. He called jni so that we can use openCv functions in android. Private static native void Canny_0 (long image_nativeObj, long edges_nativeObj, double threshold1, double threshold2, int apertureSize, boolean L2gradient); this method is used to locate the image edge. Then you can convert it into an image similar to a stroke. The parameters inside can be seen that the first parameter is the matrix of the image to be converted, the second parameter is the matrix after conversion, the third parameter is the threshold value, and the fourth parameter is, you can change the size of the two to see the effect. The larger the value, the shorter the edge month. The fifth parameter is actually the degree of edge when the display is adjusted. It seems that after I tested it, the size of these parameters needs to be adjusted by myself when it is entered, the last parameter is used to determine whether to be more accurate. Private static native void Canny_1 (long image_nativeObj, long edges_nativeObj, double threshold1, double threshold2); this is similar to the above. Private static native void GaussianBlur_0 (long src_nativeObj, long dst_nativeObj, double ksize_width, double ksize_height, double sigmaX, double sigmaY, int borderType); Gaussian filter to blur the image. The first parameter is the image prototype matrix, the second is the output matrix after conversion, the third is the width of the Gaussian filter template, and the fourth is the height of the Gaussian filter template, the fifth, sixth, sigmaX and sigmaY are the filter coefficients of Gaussian filter on the horizontal line and vertical lines, respectively. borderType is the edge point interpolation type. For the use of this method reference http://blog.csdn.net/vblittleboy/article/details/9187447 private static native void GaussianBlur_1 (long src_nativeObj, long dst_nativeObj, double ksize_width, double ksize_height, double sigmaX, double sigmaY ); private static native void trim (long src_nativeObj, long distance, double ksize_width, double ksize_height, double sigmaX); same side private static native void HoughCircles_0 (long image_nativeObj, long distance, int method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius); private static native void HoughCircles_1 (long image_nativeObj, long circles_nativeObj, int method, double dp, double minDist );

/*** Gaussian fuzzy processing ** @ return */public Bitmap gaosiBlur () {Mat rgbMat = new Mat (); Mat grayMat = new Mat (); Bitmap gaosiBitmap = Bitmap. createBitmap (mDes_Bitmap.getWidth (), mDes_Bitmap.getHeight (), Config. ARGB_8888); Utils. bitmapToMat (mDes_Bitmap, rgbMat); // convert original bitmap to Mat, r g B. size size = new Size (); // perform Gaussian fuzzy processing size. height = 17; size. width = 17; Imgproc. gaussianBlur (rgbMat, grayMat, size, 2, 2, Imgproc. BORDER_DEFAULT); Utils. matToBitmap (grayMat, gaosiBitmap); // convert mat to bitmap Log. I ("TAG", "procSrc2Gray sucess... "); return gaosiBitmap;} public Bitmap getwarpPerspective () {Mat srcMat = new Mat (); Mat dstMat = new Mat (); Mat perspectiveTransform = Imgproc. getPerspectiveTransform (srcMat, dstMat); Size size = new Size (); // perform Gaussian fuzzy processing on size. height = mDes_Bitmap.getHeight (); size. width = mDes_Bitmap.getWidth (); Bitmap dstBitmap = Bitmap. createBitmap (mDes_Bitmap.getWidth (), mDes_Bitmap.getHeight (), Config. RGB_565); Utils. bitmapToMat (mDes_Bitmap, srcMat); Core. perspectiveTransform (srcMat, dstMat, perspectiveTransform); Imgproc. warpPerspective (srcMat, dstMat, perspectiveTransform, size); Utils. matToBitmap (dstMat, dstBitmap); return dstBitmap;}/*** performs Edge Detection * @ return */public Bitmap getkan () {Mat rgbMat = new Mat (); mat grayMat = new Mat (); Bitmap grayBitmap = Bitmap. createBitmap (mDes_Bitmap.getWidth (), mDes_Bitmap.getHeight (), Config. ARGB_8888); Utils. bitmapToMat (mDes_Bitmap, rgbMat); // convert original bitmap to Mat, r g B. imgproc. canny (rgbMat, grayMat, 0,150); // performs image edge detection Utils. matToBitmap (grayMat, grayBitmap); // convert mat to bitmap Log. I ("TAG", "procSrc2Gray sucess... "); return grayBitmap ;}


Demo: http://download.csdn.net/detail/u012808234/9374169

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.