The code I wrote act like this:
- take a Bitmap
- convert it to Mat
- process the pixels
- 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