Android Bitmap,Image,Mat之间的相互转换
程序员文章站
2022-05-16 11:22:13
...
本文记录的主要是安卓java层获取到图片Image或者Bitmap,传到本地处理,
然后再把本地的图片传回java层显示
一 . JAVA层的Image图片传到本地处理,然后再传回java层显示:
//Image转成YUV Byte[]
ImageReader reader
Image image = reader.acquireLatestImage();
Image.Plane[] planes = image.getPlanes();
//注意:yBytes vBytes uBytes应该定义成全局变量。可减少GC频率
if (image.getWidth() != mConfig.getVideoWidth() ||
image.getHeight() != mConfig.getVideoHeight()) {
Log.d("Image", "WHY " + planes[0].getRowStride() + " != " + mConfig.getVideoHeight());
}
// Y-buffer
ByteBuffer yBuffer = planes[0].getBuffer();
int ySize = yBuffer.remaining();
byte[] yBytes = new byte[ySize];
yBuffer.get(yBytes);
// VU-buffer
ByteBuffer vBuffer = planes[2].getBuffer();
int vuSize = vBuffer.remaining();
byte[] vBytes = new byte[vuSize];
vBuffer.get(vBytes);
byte[] yuvBytes = null;
if (vuSize <= ySize / 4) {
ByteBuffer uBuffer = planes[1].getBuffer();
int uSize = uBuffer.remaining();
byte[] uBytes = new byte[uSize];
uBuffer.get(uBytes);
yuvBytes = ImageTool.byteMerger(yBytes, uBytes, vBytes);
} else {
yuvBytes = ImageTool.byteMerger(yBytes, vBytes);
}
获取到的byte[] yuvBytes作为参数传到c++层,同时创建一个零时Bitmap对象,传给c++层;
2. c++层获取到byte[]后,如下操作:
Java_XXX_updateHandJni(JNIEnv *env, jobject object, jbyteArray yuvdata, xxx)
jbyte *bBuffer = env->GetByteArrayElements(yuvdata, 0);
unsigned char *buf = (unsigned char *) bBuffer;
// unsigned char *转成Mat类型;
cv::cvtColor(cv::Mat(h * 3 / 2, w, CV_8UC1, buf), image, CV_YUV2RGB_NV21);
......//一系列处理
经过一系列处理后;
得到结果image;
3. 把image转成Bitmap,最后传给java层:
void MatToBitmap2
(JNIEnv *env, Mat& mat, jobject& bitmap, jboolean needPremultiplyAlpha) {
AndroidBitmapInfo info;
void *pixels = 0;
Mat &src = mat;
try {
LOGD("nMatToBitmap");
CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ||
info.format == ANDROID_BITMAP_FORMAT_RGB_565);
CV_Assert(src.dims == 2 && info.height == (uint32_t) src.rows &&
info.width == (uint32_t) src.cols);
CV_Assert(src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4);
CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
CV_Assert(pixels);
if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
Mat tmp(info.height, info.width, CV_8UC4, pixels);
if (src.type() == CV_8UC1) {
LOGD("nMatToBitmap: CV_8UC1 -> RGBA_8888");
cvtColor(src, tmp, COLOR_GRAY2RGBA);
} else if (src.type() == CV_8UC3) {
LOGD("nMatToBitmap: CV_8UC3 -> RGBA_8888");
cvtColor(src, tmp, COLOR_RGB2RGBA);
} else if (src.type() == CV_8UC4) {
LOGD("nMatToBitmap: CV_8UC4 -> RGBA_8888");
if (needPremultiplyAlpha)
cvtColor(src, tmp, COLOR_RGBA2mRGBA);
else
src.copyTo(tmp);
}
} else {
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
Mat tmp(info.height, info.width, CV_8UC2, pixels);
if (src.type() == CV_8UC1) {
LOGD("nMatToBitmap: CV_8UC1 -> RGB_565");
cvtColor(src, tmp, COLOR_GRAY2BGR565);
} else if (src.type() == CV_8UC3) {
LOGD("nMatToBitmap: CV_8UC3 -> RGB_565");
cvtColor(src, tmp, COLOR_RGB2BGR565);
} else if (src.type() == CV_8UC4) {
LOGD("nMatToBitmap: CV_8UC4 -> RGB_565");
cvtColor(src, tmp, COLOR_RGBA2BGR565);
}
}
AndroidBitmap_unlockPixels(env, bitmap);
return;
} catch (const cv::Exception &e) {
AndroidBitmap_unlockPixels(env, bitmap);
LOGE("nMatToBitmap catched cv::Exception: %s", e.what());
jclass je = env->FindClass("org/opencv/core/CvException");
if (!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
return;
} catch (...) {
AndroidBitmap_unlockPixels(env, bitmap);
LOGE("nMatToBitmap catched unknown exception (...)");
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {nMatToBitmap}");
return;
}
}
二. Bitmap传到c++层,再转Mat,然后经过处理返回Bitmap到应用层;
1. java层创建Bitmap对象,传给c++层;
Bitmap mBitmap = Bitmap.createBitmap(w,h, config);
2. c++层先把bitmap转Mat.然后再去一系列处理,处理完再使用matToBitmap传会java层;
void BitmapToMat2(JNIEnv *env, jobject& bitmap, Mat& mat, jboolean needUnPremultiplyAlpha) {
AndroidBitmapInfo info;
void *pixels = 0;
Mat &dst = mat;
try {
LOGD("nBitmapToMat");
CV_Assert(AndroidBitmap_getInfo(env, bitmap, &info) >= 0);
CV_Assert(info.format == ANDROID_BITMAP_FORMAT_RGBA_8888 ||
info.format == ANDROID_BITMAP_FORMAT_RGB_565);
CV_Assert(AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0);
CV_Assert(pixels);
dst.create(info.height, info.width, CV_8UC4);
if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGD("nBitmapToMat: RGBA_8888 -> CV_8UC4");
Mat tmp(info.height, info.width, CV_8UC4, pixels);
if (needUnPremultiplyAlpha)
cvtColor(tmp, dst, COLOR_mRGBA2RGBA);
else
tmp.copyTo(dst);
} else {
// info.format == ANDROID_BITMAP_FORMAT_RGB_565
LOGD("nBitmapToMat: RGB_565 -> CV_8UC4");
Mat tmp(info.height, info.width, CV_8UC2, pixels);
cvtColor(tmp, dst, COLOR_BGR5652RGBA);
}
AndroidBitmap_unlockPixels(env, bitmap);
return;
} catch (const cv::Exception &e) {
AndroidBitmap_unlockPixels(env, bitmap);
LOGE("nBitmapToMat catched cv::Exception: %s", e.what());
jclass je = env->FindClass("org/opencv/core/CvException");
if (!je) je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, e.what());
return;
} catch (...) {
AndroidBitmap_unlockPixels(env, bitmap);
LOGE("nBitmapToMat catched unknown exception (...)");
jclass je = env->FindClass("java/lang/Exception");
env->ThrowNew(je, "Unknown exception in JNI code {nBitmapToMat}");
return;
}
}
参考连接:https://www.cnblogs.com/Jackie-zhang/p/10084947.html
https://blog.csdn.net/lei19880402/article/details/86002482
https://www.jianshu.com/p/08dcc910b088
上一篇: Android复制到剪切板
下一篇: Mat 数据类型指针ptr 的使用