首页 > 解决方案 > 当图像保存为位图时,OpenCV 矩形是黑色的

问题描述

以下是保存前在手机屏幕上看到的图像:

在此处输入图像描述

将图像保存到外部存储后:

在此处输入图像描述

请指导如何修复opencv绘制的矩形的颜色。

下面是我的代码:

  Bitmap new_bitmap = takeScreenShot(findViewById(R.id.img));
  Bitmap workingBitmap = Bitmap.createBitmap(new_bitmap);
  Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
  File outFile = new File("External directory", "My folder name");
  FileOutputStream outStream = new FileOutputStream(outFile);
  mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
  outStream.flush();
  outStream.close();

  public Bitmap takeScreenShot(View view) {

    view.setDrawingCacheEnabled(true);
    view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_AUTO);
    view.buildDrawingCache(true);

    Bitmap image = view.getDrawingCache();

    if (image != null) {
        return Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight());
    }
    return null;
}

以下代码设置图像:

  Bitmap resultBitmap=BitmapFactory.decodeFile("Path for image");
  Mat m_ogr = new Mat(resultBitmap.getWidth(), resultBitmap.getHeight(), CvType.CV_8UC3);
  List<Rect> rectList = "Some list with Rect elements";
  for (int fi = 0; fi < rectList.size(); fi++) {
    Imgproc.rectangle(m_ogr, new Point(rectList.get(fi).x, rectList.get(fi).y), 
    new Point(rectList.get(fi).x + rectList.get(fi).width, rectList.get(fi).y + rectList.get(fi).height), 
    new Scalar(50, 205, 50), 2);
   }
  Utils.matToBitmap(m_ogr, resultBitmap);
  img.setImageBitmap(resultBitmap);

标签: androidopencvopencv3.1

解决方案


保存前如何显示图像?OpenCV 不使用 alpha 通道,在矩形位置可能为 0(例如,如果您绘制的矩形只有 3 个颜色值而不是 4 个)?


推荐阅读