0

The code I wrote act like this:

  1. take a Bitmap
  2. convert it to Mat
  3. process the pixels
  4. convert the Mat to Bitmap

At this point I have two branches. The first one displays the bitmap into an ImageView, the second one saves the bitmap on disk and then displays the bitmap into an ImageView. Despite using the same code, I'm facing different behaviours. I would like the bitmap I'm saving to preserve colors as in the first case. This is the code I'm using:

srcBitmap = ImageResizer.decodeSampledBitmapFromFile(imagePath,1024,1024,null);

src = new Mat(srcBitmap.getHeight(),srcBitmap.getWidth(),CvType.CV_8UC4);
dst = new Mat(srcBitmap.getHeight(),srcBitmap.getWidth(),CvType.CV_8UC4);

Utils.bitmapToMat(srcBitmap,src);
Utils.bitmapToMat(srcBitmap,dst);

double[] rgb;
for(i=0; i<src.height(); i++){
    for(j=0; j<src.width(); j++){
        rgb = src.get(i,j);

        r = rgb[0];
        g = rgb[1];
        b = rgb[2];
        a = rgb[3];
        .
        .
        .
        dst.put(i,j, r, g, b,a);
        }
}

Bitmap dstBitmap = Bitmap.createBitmap(srcBitmap.getWidth(),srcBitmap.getHeight(),Bitmap.Config.ARGB_8888);
Utils.matToBitmap(dst,dstBitmap);

First case: just display the bitmap

imageView = (ImageView) findViewById(R.id.bitmap_preview);                
imageView.setImageBitmap(dstBitmap);

Second case: save the bitmap and display

File newFile = fileHandler.getOutputMediaFile(FileHandler.MEDIA_TYPE_IMAGE);
FileOutputStream out = null;
try {
    out = new FileOutputStream(newFile);
    destBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
    out.flush();
    out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
imageView = (ImageView) findViewById(R.id.bitmap_preview);                
imageView.setImageBitmap(ImageResizer.decodeSampledBitmapFromFile(fileHandler.getUriFromFile(newFile).getPath(),1024,1024,null));

Thank you

sorrentix
  • 25
  • 7

1 Answers1

1

I managed to sort out the problem by changing the inizialization of the Mat this way:

    src = new Mat(bufferBitmap.getHeight(),bufferBitmap.getWidth(),CvType.CV_8U, new Scalar(4));
    dst = new Mat(bufferBitmap.getHeight(),bufferBitmap.getWidth(),CvType.CV_8U, new Scalar(4));

Edit

After saving the file make sure to call

MediaScannerConnection.scanFile(getActivity(), new String[]{file.toString()}, null, null);

So that the OS can properly index the new file, more details in this SO answer

Community
  • 1
  • 1
sorrentix
  • 25
  • 7