• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Image processing

AngryParrot

Lurker
Hello everybody.
I am working on an image encryption app.
The goal of this app : you give this app any image, it transforms it into something like this.
You can then send it to one of your contacts, who will decrypt it and recover the original image.

For now, I am just experimenting with image processing on Android, and I have a quite frustrating problem.

I need to be able to change the RGB (red/green/blue values) of the pixels, but also the alpha (transparency / opacity). My problem is with the alpha modification.

First, I did this :

Java:
    void imageProcessing()
    {
        // Creation of a "bitmap" object containing the pixels from "image.png"

        BitmapFactory bf = new BitmapFactory();
        BitmapFactory.Options bfo = new BitmapFactory.Options();

        bfo.inMutable = true;
        bfo.inPreferredConfig = ARGB_8888;

        Bitmap bm = bf.decodeFile("/storage/emulated/0/DCIM/image.png", bfo);

        // The 32 bits int which will contain values of red, green, blue, alpha
        int pixel_color;

        // The alpha, red, green, blue values
        int alpha, red, green, blue;

        // Color selected : full blue, with an alpha of 127 (the pixel will be half-transparent)
        red = 0; green = 0; blue = 255; alpha = 127;

        // Putting the A, R, G, B values into the int variable "pixel_color"
        pixel_color = (alpha<<24) | (red<<16) | (green<<8) | blue;

        // Modifying the (x=3,y=0) pixel with this color
        bm.setPixel(3, 0, pixel_color);

        // Recording the modified image as "new_image.png"

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

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

        File f = new File("/storage/emulated/0/DCIM/new_image.png");

        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(outStream.toByteArray());
            fo.flush();
            fo.close();
        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        }
    }

For a png image with an alpha canal, it works fine (some images manage RGB only, not alpha).

When I try to do the same for a jpeg image, it does not work. This is normal, jpeg does not allow to manage alpha.

So I tried to convert the jpeg into a png, then modify RGB+alpha on a pixel from this png.

Like this :

Java:
    void imageProcessing()
    {
        // Creation of a "bitmap" object containing the pixels from "parrot.jpeg"

        BitmapFactory bf1 = new BitmapFactory();
        BitmapFactory.Options bfo1 = new BitmapFactory.Options();

        bfo1.inMutable = true;
        bfo1.inPreferredConfig = ARGB_8888;
        bfo1.outConfig = ARGB_8888;

        Bitmap bm1 = bf1.decodeFile("/storage/emulated/0/DCIM/parrot.jpeg", bfo1);

        // Recording the image with the png format, as "parrot.png"

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        bm1.compress(Bitmap.CompressFormat.PNG, 100, outStream);

        File f = new File("/storage/emulated/0/DCIM/parrot.png");

        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(outStream.toByteArray());
            fo.flush();
            fo.close();
        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        }

        // Creation of a "bitmap" object containing the pixels from "parrot.png"

        BitmapFactory bf2 = new BitmapFactory();
        BitmapFactory.Options bfo2 = new BitmapFactory.Options();

        bfo2.inMutable = true;
        bfo2.inPreferredConfig = ARGB_8888;
        bfo2.outConfig = ARGB_8888;

        Bitmap bm2 = bf2.decodeFile("/storage/emulated/0/DCIM/parrot.png", bfo2);

        // The 32 bits int which will contain values of red, green, blue, alpha
        int pixel_color;

        // The alpha, red, green, blue values
        int alpha, red, green, blue;

        // Color selected : full blue, with an alpha of 127 (the pixel will be half-transparent)
        red = 0; green = 0; blue = 255; alpha = 127;

        // Putting the A, R, G, B values into the int variable "pixel_color"
        pixel_color = (alpha<<24) | (red<<16) | (green<<8) | blue;

        // Modifying the (x=3,y=0) pixel with this color
        bm2.setPixel(3, 0, pixel_color);

        // Recording the modified image as "new_parrot.png"

        ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();

        bm2.compress(Bitmap.CompressFormat.PNG, 100, outStream2);

        File f2 = new File("/storage/emulated/0/DCIM/new_parrot.png");

        try {
            f2.createNewFile();
            FileOutputStream fo = new FileOutputStream(f2);
            fo.write(outStream2.toByteArray());
            fo.flush();
            fo.close();
        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
        }
    }

However, the modified pixel on the final image is blue (like I planned), but with an alpha of 255 (instead of 127).

Do some of you have any idea about this issue?
 
Back
Top Bottom