Android Jni OpenCVによる画像サイズのスケーリング(一)
2561 ワード
一、上javaコード
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
}
二、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) {
}
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");
}
@Override
protected 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);
@Override
public 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 BGRA
Mat 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;
}