Kkk.
Kkk.

Reputation: 1

Java android change black color in picture to tracspadence and save in file

Hi I want to rotate my image and save in file I did this :

for (int i = 0; i < 361; i++) {
    Bitmap bm = RotateMyBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.znacznik_new), i);
    String path = Environment.getExternalStorageDirectory().toString();
    OutputStream fOut = null;
    Integer counter = 0;
    File file = new File(path, "ikona"+i+".jpg"); // the File to save , append increasing numeric counter to prevent files from getting overwritten.
    try {
        fOut = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }


    bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
    try {
        fOut.flush(); // Not really required
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        fOut.close(); // do not forget to close the stream
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}


   public static Bitmap RotateMyBitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    }

And I rotate image and I have files but a image have a black background and my oryginal image doesn't have a black background it have transpadence . How I can change a black color to transpadence and save in file

Upvotes: 2

Views: 262

Answers (1)

wrkwrk
wrkwrk

Reputation: 2351

JPEG doesn't support transparency, all the transparent parts will turn black. Compress the bitmap with Bitmap.CompressFormat.PNG.

bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);

JPEG black, PNG transparent

Upvotes: 1

Related Questions