一、要理解还是得自己看代码
1.1 完整的测试代码路径如下
https://download.csdn.net/download/qq_37858386/87916944
1.2 代码架构
1.3 app 运行效果
二、android studio 添加 opencv module可以参考下面的文章,比较详细。
Android + OpenCV 入门教程笔记(保姆级)_opencv android_派的十二位的博客-CSDN博客
三、按上面的添加opencv 模块后,添加文字,方法一,不需要另外添加jni。这个方法主要参考下面文章Android App开发超实用实例 | OpenCV在Android手机实现在图像上添加文本_opencv手机版_TiAmo zhang的博客-CSDN博客
1、添加下面的代码就可以在图片上添加文字。
myImageView = (ImageView) findViewById(R.id.img_picture);
myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.win);
// myImageView.setImageBitmap(myBitmap);
Bitmap myNewBitmap=Bitmap.createBitmap(myBitmap.getWidth(),
myBitmap.getHeight(),Bitmap.Config.RGB_565); //根据原始图像创建空Bitmap
Mat myMat=new Mat();
Utils.bitmapToMat(myBitmap,myMat); //将原始图像保存至myMat
Point myPoint=new Point(0, 100); //定义绘制文本的起始位置
Scalar myScalar=new Scalar(255,0,0,0); //设置绘制文本的颜色
//在图像上添加文本(仅支持英文、符号等,中文暂不支持)
Imgproc.putText(myMat,"I Love OpenCV",myPoint,
1,5,myScalar,3);
//将添加文本之后的图像输出至myNewBitmap
Utils.matToBitmap(myMat,myNewBitmap);
//通过ImageView控件显示叠加文本之后的图像
//myNewBitmap.
myImageView.setImageBitmap(myNewBitmap);
static{
System.loadLibrary("opencv_java4");
}
2、效果图如下。
四、方法二、使用jni 调用opencv 实现在图片上添加文字。主要参考下面文章【安卓随笔】引入OpenCV进行NDK开发之图片传递(案例:文字水印)_vicemiami的博客-CSDN博客
1、java 主要代码
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.win);
int w = bmp.getWidth();
int h = bmp.getHeight();
int[] pixels = new int[w * h];
bmp.getPixels(pixels, 0, w, 0, 0, w, h);
String content = "I love New zealand";
int[] resultInt = addText2Picture(pixels, w, h, content);
if (resultInt != null) {
Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
myImageView.setImageBitmap(resultImg);
}
}
2、jni 代码
JNIEXPORT jintArray JNICALL
Java_com_hypernano_opencvdemo_MainActivity_addText2Picture(
JNIEnv *env,
jobject, jintArray pixels_,
jint w, jint h, jstring textString) {
const char *text = env->GetStringUTFChars(textString, 0);
string content = text;
jint *pixels = env->GetIntArrayElements(pixels_, NULL);
if (pixels == NULL) {
return NULL;
}
Mat src(h, w, CV_8UC4, pixels);
int width = src.cols;
int height = src.rows;
int margin = 10;
int baseline;
Size srcSize = getTextSize(content, FONT_HERSHEY_COMPLEX, 2, 2, &baseline);
cv::Point point;
point.x = width - srcSize.width - margin;
point.y = height - margin;
//Scalar BGR
putText(src, content, point, FONT_HERSHEY_COMPLEX, 2, cv::Scalar(94, 206, 165, 255), 2, 8, 0);
int size = w * h;
jintArray result = env->NewIntArray(size);
env->SetIntArrayRegion(result, 0, size, pixels);
env->ReleaseIntArrayElements(pixels_, pixels, 0);
env->ReleaseStringUTFChars(textString, text);
return result;
}
3、效果图
五、对图片进行模糊处理效果,主要参考下面的文章
Android引入OpenCV的示例_Android_脚本之家
1、java部分的主要代码
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.win);
int w = srcBitmap.getWidth();
int h = srcBitmap.getHeight();
int[] pixels = new int[w*h];
srcBitmap.getPixels(pixels,0,w,0,0,w,h);
int[] outPixels = getBlurImage(pixels ,w,h);
Bitmap destBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
destBitmap.setPixels(outPixels,0,w,0,0,w,h);
myImageView = (ImageView) findViewById(R.id.img_picture);
myImageView.setImageBitmap(destBitmap);
2、jni 部分主要代码
JNIEXPORT jintArray JNICALL
//Java_com_live_longsiyang_opencvdemo_OpenCVNativeManager_getBlurImage
Java_com_hypernano_opencvdemo_MainActivity_getBlurImage
(JNIEnv *env, jobject thiz,jintArray pixels_, jint w,jint h) {
int colorType = CV_8UC4;
jint *pixels = env->GetIntArrayElements(pixels_, NULL);
Mat imgMat(h, w, colorType, pixels);
Mat out(h, w, colorType);
blur(imgMat, out, Size(20, 20));
env->ReleaseIntArrayElements(pixels_, pixels, 0);
int *outIntImage = new int[w * h];
for (int i = 0; i < w * h; i++) {
int a, r, g, b;
for (int j = 0; j < 4; j++) {
a = out.data[i * 4 + 3];
r = out.data[i * 4 + 2];
g = out.data[i * 4 + 1];
b = out.data[i * 4 + 0];
outIntImage[i] = (int) out.data[i * 4 + j];
}
outIntImage[i] = MAKE_ARGB(a,r,g,b);
}
jintArray result = env->NewIntArray(w * h);
env->SetIntArrayRegion(result, 0, w * h, outIntImage);
return result;
}
3、效果图,可以看到图片已经模糊
六、参考文章
android 图片添加暗水印 - 掘金
Android引入OpenCV的示例_Android_脚本之家
用opencv在图片上面添加水印_添加水印opencv qt_三火哥的博客-CSDN博客
Android OpenCV实现图片叠加,水印_xkyle的博客-CSDN博客
Android + OpenCV 入门教程笔记(保姆级)_opencv android_派的十二位的博客-CSDN博客
OpenCV download | SourceForge.net
安卓用jni对接opencv踩坑记_"the imported target \"libcpufeatures\" references_IceGost的博客-CSDN博客
Android 配置 OpenCV环境(Android Stdio 4.1.3 openCV 4.5.5)_opencv环境 androis_HGGshiwo的博客-CSDN博客