GITHUB:https://github.com/hyyz3293/DarkWatermark  -------------------------

 

第一步:android studio 配置 opcv环境 https://opencv.org/opencv-3-4-1/

OPCV 环境配置:
a:下载安卓opcv:  https://opencv.org/opencv-3-4-1/  (我用的是openCVLibrary341)
 
b:将下载好后的(sdk\java)    导入 android studio 项目中: File ->  New -> Import Module  

c:删除module中的  <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="21" />  
  更改项目项目配置 与你的一样
d:将module中libs里面的架包 (OpenCV-android-sdk\sdk\native\libs) 拷贝入自己的项目中 
 
e:自己项目加入配置 让app包,与新添加的这个OpenCV库关联
   implementation project(':openCVLibrary341')
     sourceSets {
     main {
        //说明so的路径为该libs路径,关联所有地图SDK的so文件
        jniLibs.srcDir 'libs'
    }
  }
  
 f: 加入 //以下很重要
  //将添加的.so文件,打包成jar
  task nativeLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
      destinationDir file("$buildDir/native-libs")
      baseName 'native-libs'
      from fileTree(dir: 'libs', include: '**/*.so')
      into 'lib/'
  }
  tasks.withType(JavaCompile) {
      compileTask -> compileTask.dependsOn(nativeLibsToJar)
  }

第二步:判断Opcv是否集成成功 //初始化 if (OpenCVLoader.initDebug())

    可根据灰色测试按钮测试
    
    Mat src = new Mat();//Mat是OpenCV的一种图像格式
    Mat temp = new Mat();
    Mat dst = new Mat();
    Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.a);
    Utils.bitmapToMat(bitmap, src);
    Imgproc.cvtColor(src, temp, Imgproc.COLOR_RGB2BGR);
    Imgproc.cvtColor(temp, dst, Imgproc.COLOR_BGR2GRAY);
    Utils.matToBitmap(dst, bitmap);
    img.setImageBitmap(bitmap);
    src.release();
    temp.release();
    dst.release();

第三步: 开始写代码了

    1、添加水印 并保存图片到本地哟

    Bitmap bt = ImgUtils.drawableToBitmap(getResources().getDrawable(R.mipmap.a));
    Mat src = new Mat(bt.getHeight(), bt.getWidth(), CvType.CV_8U);
    Utils.bitmapToMat(bt, src);

    Mat imageMat = OpcvImgUtils.addImageWatermarkWithText(src, "Jack666");
    Bitmap bt3 = null;
    bt3 = Bitmap.createBitmap(imageMat.cols(), imageMat.rows(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(imageMat, bt3);


   File root = new File(Environment.getExternalStorageDirectory().getAbsoluteFile(), "WaterMark");
   if ((root.exists() || root.mkdir()) && root.isDirectory()) {
        filePath = root.getAbsolutePath();
    }
   path = filePath + "/" + ImgUtils.getTimeStampFileName(0);
   ImageUtils.save(bt3, path,  Bitmap.CompressFormat.PNG);
   LogUtils.e(path);
   Glide.with(this).load(path).into(imgTest);
   
   
   
   2、从本地图片中 提取水印哟
   
         Bitmap bitmap = BitmapFactory.decodeFile(path);
        Mat temp = new Mat();
        Utils.bitmapToMat(bitmap, temp);

        Mat showMat = OpcvImgUtils.getImageWatermarkWithText(temp);
        Bitmap  bt4 = Bitmap.createBitmap(showMat.cols(), showMat.rows(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(showMat, bt4);

        imgTest2.setImageBitmap(bt4);
        String paths = filePath + "/" + ImgUtils.getTimeStampFileName(0);
        ImageUtils.save(bt4, paths ,  Bitmap.CompressFormat.PNG);
          Bitmap bitmap = BitmapFactory.decodeFile(path);
        Mat temp = new Mat();
        Utils.bitmapToMat(bitmap, temp);

        Mat showMat = OpcvImgUtils.getImageWatermarkWithText(temp);
        Bitmap  bt4 = Bitmap.createBitmap(showMat.cols(), showMat.rows(), Bitmap.Config.RGB_565);
        Utils.matToBitmap(showMat, bt4);

        imgTest2.setImageBitmap(bt4);
        String paths = filePath + "/" + ImgUtils.getTimeStampFileName(0);
        ImageUtils.save(bt4, paths ,  Bitmap.CompressFormat.PNG);
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐