1. Are you ready for the Galaxy S20? Here is everything we know so far!

Image processing

Discussion in 'Android Development' started by AngryParrot, Apr 18, 2020.

  1. AngryParrot

    AngryParrot Lurker
    Thread Starter

    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 :

    Code (Java):
    1.     void imageProcessing()
    2.     {
    3.         // Creation of a "bitmap" object containing the pixels from "image.png"
    4.  
    5.         BitmapFactory bf = new BitmapFactory();
    6.         BitmapFactory.Options bfo = new BitmapFactory.Options();
    7.  
    8.         bfo.inMutable = true;
    9.         bfo.inPreferredConfig = ARGB_8888;
    10.  
    11.         Bitmap bm = bf.decodeFile("/storage/emulated/0/DCIM/image.png", bfo);
    12.  
    13.         // The 32 bits int which will contain values of red, green, blue, alpha
    14.         int pixel_color;
    15.  
    16.         // The alpha, red, green, blue values
    17.         int alpha, red, green, blue;
    18.  
    19.         // Color selected : full blue, with an alpha of 127 (the pixel will be half-transparent)
    20.         red = 0; green = 0; blue = 255; alpha = 127;
    21.  
    22.         // Putting the A, R, G, B values into the int variable "pixel_color"
    23.         pixel_color = (alpha<<24) | (red<<16) | (green<<8) | blue;
    24.  
    25.         // Modifying the (x=3,y=0) pixel with this color
    26.         bm.setPixel(3, 0, pixel_color);
    27.  
    28.         // Recording the modified image as "new_image.png"
    29.  
    30.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    31.  
    32.         bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    33.  
    34.         File f = new File("/storage/emulated/0/DCIM/new_image.png");
    35.  
    36.         try {
    37.             f.createNewFile();
    38.             FileOutputStream fo = new FileOutputStream(f);
    39.             fo.write(outStream.toByteArray());
    40.             fo.flush();
    41.             fo.close();
    42.         } catch (FileNotFoundException e) {
    43.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    44.         } catch (IOException e) {
    45.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    46.         }
    47.     }
    48.  
    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 :

    Code (Java):
    1.     void imageProcessing()
    2.     {
    3.         // Creation of a "bitmap" object containing the pixels from "parrot.jpeg"
    4.  
    5.         BitmapFactory bf1 = new BitmapFactory();
    6.         BitmapFactory.Options bfo1 = new BitmapFactory.Options();
    7.  
    8.         bfo1.inMutable = true;
    9.         bfo1.inPreferredConfig = ARGB_8888;
    10.         bfo1.outConfig = ARGB_8888;
    11.  
    12.         Bitmap bm1 = bf1.decodeFile("/storage/emulated/0/DCIM/parrot.jpeg", bfo1);
    13.  
    14.         // Recording the image with the png format, as "parrot.png"
    15.  
    16.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    17.  
    18.         bm1.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    19.  
    20.         File f = new File("/storage/emulated/0/DCIM/parrot.png");
    21.  
    22.         try {
    23.             f.createNewFile();
    24.             FileOutputStream fo = new FileOutputStream(f);
    25.             fo.write(outStream.toByteArray());
    26.             fo.flush();
    27.             fo.close();
    28.         } catch (FileNotFoundException e) {
    29.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    30.         } catch (IOException e) {
    31.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    32.         }
    33.  
    34.         // Creation of a "bitmap" object containing the pixels from "parrot.png"
    35.  
    36.         BitmapFactory bf2 = new BitmapFactory();
    37.         BitmapFactory.Options bfo2 = new BitmapFactory.Options();
    38.  
    39.         bfo2.inMutable = true;
    40.         bfo2.inPreferredConfig = ARGB_8888;
    41.         bfo2.outConfig = ARGB_8888;
    42.  
    43.         Bitmap bm2 = bf2.decodeFile("/storage/emulated/0/DCIM/parrot.png", bfo2);
    44.  
    45.         // The 32 bits int which will contain values of red, green, blue, alpha
    46.         int pixel_color;
    47.  
    48.         // The alpha, red, green, blue values
    49.         int alpha, red, green, blue;
    50.  
    51.         // Color selected : full blue, with an alpha of 127 (the pixel will be half-transparent)
    52.         red = 0; green = 0; blue = 255; alpha = 127;
    53.  
    54.         // Putting the A, R, G, B values into the int variable "pixel_color"
    55.         pixel_color = (alpha<<24) | (red<<16) | (green<<8) | blue;
    56.  
    57.         // Modifying the (x=3,y=0) pixel with this color
    58.         bm2.setPixel(3, 0, pixel_color);
    59.  
    60.         // Recording the modified image as "new_parrot.png"
    61.  
    62.         ByteArrayOutputStream outStream2 = new ByteArrayOutputStream();
    63.  
    64.         bm2.compress(Bitmap.CompressFormat.PNG, 100, outStream2);
    65.  
    66.         File f2 = new File("/storage/emulated/0/DCIM/new_parrot.png");
    67.  
    68.         try {
    69.             f2.createNewFile();
    70.             FileOutputStream fo = new FileOutputStream(f2);
    71.             fo.write(outStream2.toByteArray());
    72.             fo.flush();
    73.             fo.close();
    74.         } catch (FileNotFoundException e) {
    75.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    76.         } catch (IOException e) {
    77.             Log.w("TAG", "Error saving image file: " + e.getMessage());
    78.         }
    79.     }
    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?
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Image processing
  1. dkperez
    Replies:
    1
    Views:
    112
  2. wonderinginandroid
    Replies:
    1
    Views:
    216
  3. Challenged_101
    Replies:
    1
    Views:
    156
  4. Cari McFall
    Replies:
    2
    Views:
    397
  5. lister171254
    Replies:
    1
    Views:
    518
  6. spottydog10
    Replies:
    4
    Views:
    241
  7. mrop
    Replies:
    5
    Views:
    506
  8. QCKiwi
    Replies:
    2
    Views:
    332
  9. Mohamed13
    Replies:
    6
    Views:
    801
  10. Negrazo1949
    Replies:
    0
    Views:
    514

Share This Page

Loading...