Bitmap loses transparency when saved

I have a problem when I try to save a bitmap in an external image directory. When I use the Bitmap.compress function to save it, the bitmap loses transparency and makes the background black. But when I pass the bitmap to the image and show it in action, it looks great and has transparency. It's only when I try to save it that the transparency turns black.

I have to say that im using two bitmaps and porterduff mode, draw the path on the bitmap and only show the image in the drawn path and all other pixels should be clipped or transparent.

So here's the function to create a bitmap of the path:

private void createPathBitmap(RectF rect, Bitmap bitmap, Path path) {
    RectF tmpRect = new RectF(rect);
    Bitmap src = Bitmap.createBitmap(bitmap, (int) tmpRect.left, (int) tmpRect.top, (int) tmpRect.width(), (int) tmpRect.height());
    Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dst);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(mDisplayDensity * SnippetLayer.PATH_DIAMETER);
    path.offset(-rect.left, -rect.top);
    canvas.drawPath(path, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    RectF srcRect = new RectF(0, 0, rect.width(), rect.height());
    canvas.drawBitmap(src, null, srcRect, paint);
    BitmapManager.sBitmapSnippet = dst;
}

      

And here's a way to save that bitmap for external storage:

 SimpleDateFormat dateFormat = new SimpleDateFormat("HH_mm_ss_dd_MM_yyyy");
        File snippetFile = new File(picDir, fileName+"_"+dateFormat.format(new Date())+".png");
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(snippetFile);
            BitmapManager.sBitmapSnippet.setHasAlpha(true);
            BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

      

The image only appears in the path, and the rest of the bounding box is black and opaque. I appreciate any help.

+6


source to share


4 answers


I am using the compress () method to write a bitmap to the output stream:

bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

      



It is important to use PNG format. JPEG converts transparent background to black.

+9


source


I don't know exactly why it is losing transparency, but I had the same problem. All you have to do is change

BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);

in



BitmapManager.sBitmapSnippet.compress(Bitmap.CompressFormat.PNG, 0, fileOutputStream);

,

This will compress the bitmap at full quality, but with transparent areas.

+4


source


Don't lose transparency with this little piece

The problem here is that you are saving the image in .JPEG format, so the JPEG takes black background as transparent, so we save it in .PNG format, so we definitely get transparent

private void saveImage(Bitmap data, View view) {
    File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Magic PhotoShoot");
    if (!createFolder.exists())
        createFolder.mkdir();
    Calendar c = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String strDate = sdf.format(c.getTime());
    File saveImage = new File(createFolder, "Photoshoot_" + strDate + ".png");
    try {
        OutputStream outputStream = new FileOutputStream(saveImage);
        data.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        outputStream.flush();
        outputStream.close();
        Snackbar.make(view, "Saved to PhotoShoot successfully", Snackbar.LENGTH_SHORT).show();
        isSave = true;
        Glob.savedImage = saveImage.getAbsolutePath();
        MediaStore.Images.Media.insertImage(getContentResolver(), saveImage.getAbsolutePath(), saveImage.getName(), saveImage.getName());
    } catch (FileNotFoundException e) {
        Snackbar.make(view, "File not found", Snackbar.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Snackbar.make(view, "Error while saving image", Snackbar.LENGTH_SHORT).show();
        e.printStackTrace();
    }
}

      

+1


source


You have to use Bitmap.CompressFormat.PNG or Bitmap.CompressFormat.WEBP. If you want a smaller image size. You have to use Bitmap.CompressFormat.WEBP like I did.

enter image description here

0


source







All Articles