Android Jni 利用OpenCV 实现图像尺寸缩放(一)

来源:互联网 发布:数控车床计算软件下载 编辑:程序博客网 时间:2024/06/16 17:47

一,上java代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button btnProc;private ImageView imageView;private Bitmap bmp;// Used to load the 'native-lib' library on application startup.static {    System.loadLibrary("native-lib");}@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    // Example of a call to a native method    btnProc = (Button) findViewById(R.id.btn_gray_process);    imageView = (ImageView) findViewById(R.id.image_view);    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test5);    imageView.setImageBitmap(bmp);    btnProc.setOnClickListener(this);}/** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */public static native int[] grayProc(int[] pixels, int w, int h);@Overridepublic void onClick(View view) {    int w = bmp.getWidth();    int h = bmp.getHeight();    int[] pixels = new int[w*h];    bmp.getPixels(pixels, 0, w, 0, 0, w, h);    long startTime = System.currentTimeMillis();    int[] resultInt = grayProc(pixels, w, h);    long endTime = System.currentTimeMillis();    w = w/2;    h = h/2;    Log.e("JNITime",""+(endTime-startTime));    Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);    //(@ColorInt int[] pixels, int offset, int stride,int x, int y, int width, int height)    resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);    imageView.setImageBitmap(resultImg);}

}

二,实现jni方法(利用resize())

extern “C”
JNIEXPORT jintArray JNICALL
Java_com_example_dgxq008_opencv_1readpixel_MainActivity_grayProc(JNIEnv *env, jclass type
, jintArray pixels_
, jint w
, jint h) {

jint* pixels = env->GetIntArrayElements(pixels_, NULL);if (pixels==NULL){    return 0;}//图片一进来时是ARGB  通过mat转换BGRAMat img(h,w,CV_8UC4,(uchar *)pixels);  //pixels 操作的是同一份数据Mat temp;//进行尺寸调整操作//( InputArray src, OutputArray dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR );//要特别注意对应 Size_(_Tp _width, _Tp _height);resize(img,temp,Size( img.cols/2, img.rows/2 ));//对应数据指针uchar* ptrTemp = temp.data;int size = w*h/4;jintArray result = env->NewIntArray(size);env->SetIntArrayRegion(result,0,size,(jint*)ptrTemp);env->ReleaseIntArrayElements(pixels_, pixels, 0);return result;

}

原创粉丝点击