×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Question [ASK] how to save image file

    Hi all, i have a problem here.
    i have read about saving image, but i cant do it.
    i have a .jpg image, and i want to get the rgb value of every pixel. I have make the code, but some of the pixel the values are different than the original. How to fix it?
    Fyi, i can save it but when i open the file it wont be open.

    Can anyone please help me? I'm waiting for answers. Please help.

  2. #2
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: [ASK] how to save image file

    Hi tbpjap and welcome to Nokia Developer Discussion Boards!

    How are you trying to save and read the image? If you are using FileConnection here's a small code snippet how to read the file.

    Code:
          try {
                FileConnection fileConn = (FileConnection) Connector.open(imageName, Connector.READ);
    
                InputStream fis = fileConn.openInputStream();
                int fileSize = (int) fileConn.fileSize();
    
                byte[] imageData = new byte[fileSize];
    
                fis.read(imageData, 0, fileSize);
    
                fis.close();
                fileConn.close();
    
                Image image = Image.createImage(imageData, 0, fileSize);
    
            } catch (IOException e) {
               
            }
    How are the pixels different from the original one? When using getRGB on devices that do not support alpha blending, the alpha value will be 0xFF for opaque pixels.On devices that support alpha blending, alpha channel values may be resampled to reflect the number of levels of semitransparency supported.

    -tiviinik

  3. #3
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    thanks a lot tiviinik..
    Last edited by tbpjap; 2012-03-28 at 02:13.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    thanks a lot for your response, tiviinik..
    i am using FileConnection, and i can read the image succesfully.

    But, how can i get every pixel values of the image?
    can you please help me to solve my problem?
    (what i'm trying to do:
    image --> get every pixel color value in int[] --> convert the value to binary --> do some modification -->
    convert back to int[] --> save as new image)

    here is some of my code (i have read the image using FileConnection):
    Code:
            int w= image.getWidth();
            int h= image.getHeight();
            int[] rgbData = new int[panjang*tinggi];
            int[] red = new int[rgbData.length];
            int[] green = new int[rgbData.length];
            int[] blue = new int[rgbData.length];
    
            image.getRGB(rgbData, 0, w, 0, 0, w, h);
            for(int i=0;i<rgbData.length;i++){
                red[i] = (rgbData[i] & 0xff0000) >> 16;
                green[i] = (rgbData[i] & 0x00ff00) >> 8;
                blue[i] = rgbData[i] & 0x0000ff;
            }
    
            //convert into binary
            String[] binImageRed = new String[rgbData.length];
            String[] binImageGreen = new String[rgbData.length];
            String[] binImageBlue = new String[rgbData.length];
            for(int i=0;i<rgbData.length;i++){
                binImageRed[i] = Integer.toBinaryString(red[i]);
                binImageGreen[i] = Integer.toBinaryString(green[i]);
                binImageBlue[i] = Integer.toBinaryString(blue[i]);
                while(binImageRed[i].length()<8)
                    binImageRed[i] = "0"+binImageRed[i];
                while(binImageGreen[i].length()<8)
                    binImageGreen[i] = "0"+binImageGreen[i];
                while(binImageBlue[i].length()<8)
                    binImageBlue[i] = "0"+binImageBlue[i];
            } 
    
            //convert to int[]
            int[] redNew = new int[rgbData.length];
            int[] greenNew = new int[rgbData.length];
            int[] blueNew = new int[rgbData.length];
            for(int i=0;i<rgbData.length;i++){
                redNew[i] = Integer.parseInt(binImageRed[0],2);
                greenNew[i] = Integer.parseInt(binImageGreen[0],2);
                blueNew[i] = Integer.parseInt(binImageBlue[0],2);
            }
    Please help me,
    i will wait for your reply..

    -tbpjap

  5. #5
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: [ASK] how to save image file

    Hi tbpjap,

    Here's a sample how to convert it back to int[] and create the image.

    Code:
    for(int i=0;i<rgbData.length;i++){
          rgbData[i] = (0xFF<<24) + (redNew[i]<<16) + (greenNew[i]<<8) + (blueNew[i]);;
    }
    
    Image image = Image.createRGBImage(rgbData, w, h, true);
    -tiviinik

  6. #6
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    Quote Originally Posted by tiviinik View Post
    Hi tbpjap,

    Here's a sample how to convert it back to int[] and create the image.

    Code:
    for(int i=0;i<rgbData.length;i++){
          rgbData[i] = (0xFF<<24) + (redNew[i]<<16) + (greenNew[i]<<8) + (blueNew[i]);;
    }
    
    Image image = Image.createRGBImage(rgbData, w, h, true);
    -tiviinik
    thankyou for your reply.
    i've done it tiviinik, but it give me a blue image - i mean the same image but only in blue color.
    how can i fix it?

    -tbpjap

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    Quote Originally Posted by tiviinik View Post
    Hi tbpjap,

    Here's a sample how to convert it back to int[] and create the image.

    Code:
    for(int i=0;i<rgbData.length;i++){
          rgbData[i] = (0xFF<<24) + (redNew[i]<<16) + (greenNew[i]<<8) + (blueNew[i]);;
    }
    
    Image image = Image.createRGBImage(rgbData, w, h, true);
    -tiviinik
    thankyou for your reply.
    i've done it tiviinik, but it give me a blue image - i mean the same image but only in blue color.
    how can i fix it?

    -tbpjap

  8. #8
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: [ASK] how to save image file

    Hi tbpjap,

    In your original code snippet you posted you are always inserting only the first value of the binary data to your new color values. See code below.

    Code:
            for(int i=0;i<rgbData.length;i++){
                redNew[i] = Integer.parseInt(binImageRed[0],2); // change to Integer.parseInt(binImageRed[i],2); etc.
                greenNew[i] = Integer.parseInt(binImageGreen[0],2);
                blueNew[i] = Integer.parseInt(binImageBlue[0],2);
            }
    If that is not the problem could you please post your current code. It would help to solve the problem.

    -tiviinik

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    thanks, tiviniik. That's my mistake, and i have done it by making:

    Code:
    for(int i=0;i<rgbData.length;i++){
                redNew[i] = Integer.parseInt(binImageRed[i],2);
                greenNew[i] = Integer.parseInt(binImageGreen[i],2);
                blueNew[i] = Integer.parseInt(binImageBlue[i],2);
            }
    now i have the new rgbData as an int array.
    How can i convert it back to image?

    i have done this:
    Code:
    rgbDataNew[i] = (0xff<<24) | (redNew[i]<<16) | (greenNew[i]<<8) | (blueNew[i]);
    Code:
    Image imgNew = Image.createRGBImage(rgbDataNew, w, h, true);
    if i check the values of rgbDataNew[] using System.out.println(), it gives me the correct value.
    But if i check the rgbData of the imgNew, it gives me the rgbData from the original image.
    How can i solve this?

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    i'm begging for helps..

  11. #11
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: [ASK] how to save image file

    Hi tbpjap,

    If you use createRGBImage to create the image from rgbDataNew, then the new image should be the one that is generated from the ARGB data. So there might be some errors when you are modifying your data. Could you post the current code snippet that you are using to help solve the problem.

    -tiviinik

  12. #12
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    Hi tiviniik, thanks for your reply. My image is in variable image.
    This is my code:

    Code:
    //START HERE
            int w = image.getWidth();
            int h = image.getHeight();
            int[] rgbData = new int[w*h];
            int[] alpha = new int[rgbData.length];
            int[] red = new int[rgbData.length];
            int[] green = new int[rgbData.length];
            int[] blue = new int[rgbData.length];
            image.getRGB(rgbData, 0, w, 0, 0, w, h);
    
            for(int i=0;i<rgbData.length;i++){
                alpha[i] = (rgbData[i] & 0xff000000) >> 24;
                red[i] = (rgbData[i] & 0xff0000) >> 16;
                green[i] = (rgbData[i] & 0x00ff00) >> 8;
                blue[i] = (rgbData[i] & 0x0000ff) >> 0;
                rgbData[i] = (alpha[i]<<24) | (red[i]<<16) | (green[i]<<8) | (blue[i]);
            }//rgb of image 
    
            //convert rgbData[] to binary
            String[] binImageRed = new String[rgbData.length];
            String[] binImageGreen = new String[rgbData.length];
            String[] binImageBlue = new String[rgbData.length];
            for(int i=0;i<rgbData.length;i++){
                binImageRed[i] = Integer.toBinaryString(red[i]);
                binImageGreen[i] = Integer.toBinaryString(green[i]);
                binImageBlue[i] = Integer.toBinaryString(blue[i]);
                while(binImageRed[i].length()<8)
                    binImageRed[i] = "0"+binImageRed[i];
                while(binImageGreen[i].length()<8)
                    binImageGreen[i] = "0"+binImageGreen[i];
                while(binImageBlue[i].length()<8)
                    binImageBlue[i] = "0"+binImageBlue[i];
            }
    
            int k=0; int j=0;
            for(int i=0; i<(bin.length*7)/3;i++){    //bin is variable for text, example: 100001 
                binImageRed[i] = replaceCharAt(binImageRed[i], 7, bin[k].charAt(j));
                j++;
                if(j>6){
                    k++;
                    j=0;
                }
                binImageGreen[i] = replaceCharAt(binImageGreen[i], 7, bin[k].charAt(j));
                j++;
                if(j>6){
                    k++;
                    j=0;
                }
                binImageBlue[i] = replaceCharAt(binImageBlue[i], 7, bin[k].charAt(j));
                j++;
                if(j>6){
                    k++;
                    j=0;
                }
            }  //i have the new binRGB
    
    
            //convert binary to int
            int[] rgbDataNew = new int[w*h];
            int[] redNew = new int[rgbDataNew.length];
            int[] greenNew = new int[rgbDataNew.length];
            int[] blueNew = new int[rgbDataNew.length];
            for(int i=0;i<rgbDataNew.length;i++){
                redNew[i] = Integer.parseInt(binImageRed[i],2);
                greenNew[i] = Integer.parseInt(binImageGreen[i],2);
                blueNew[i] = Integer.parseInt(binImageBlue[i],2);
                rgbDataNew[i] = (alpha[i]<<24) | (redNew[i]<<16) | (greenNew[i]<<8) | (blueNew[i]);
            } //we have the new rgbData[]
    
    
            //create new image 
            Image imgNew = Image.createRGBImage(rgbDataNew, w, h, false);
            
            //now i want to check the value of rgbData[]
            int w1 = imgNew.getWidth();
            int h1 = imgNew.getHeight();
            int[] rgbData1 = new int[w1*h1];
            int[] argb = new int[rgbData.length];
            int[] red1 = new int[rgbData1.length];
            int[] green1 = new int[rgbData1.length];
            int[] blue1 = new int[rgbData1.length];
            imgNew.getRGB(rgbData1, 0, w1, 0, 0, w1, h1);
    
            for(int i=0;i<rgbData1.length;i++){
                alpha1[i] = (rgbData1[i] & 0xff000000) >> 24;
                red1[i] = (rgbData1[i] & 0xff0000) >> 16;
                green1[i] = (rgbData1[i] & 0x00ff00) >> 8;
                blue1[i] = (rgbData1[i] & 0x0000ff) >> 0;
                rgbData1[i] = (alpha1[i]<<24) | (red1[i]<<16) | (green1[i]<<8) | (blue1[i]);
            }   //from here, i know that rgbData1 has rgbData from original file, not the newRgbData
    please help me..

    regards,
    tbpjap

  13. #13
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    i have another condition tiviinik.
    if i add more code like this, this is SECOND CODE:

    Code:
            ///HERE IS THE DIFFERENT//
            //convert binary to int
            int[] rgbDataNew = new int[w*h];
            int[] redNew = new int[rgbDataNew.length];
            int[] greenNew = new int[rgbDataNew.length];
            int[] blueNew = new int[rgbDataNew.length];
            for(int i=0;i<rgbDataNew.length;i++){
                redNew[i] = Integer.parseInt(binImageRed[i],2);
                greenNew[i] = Integer.parseInt(binImageGreen[i],2);
                blueNew[i] = Integer.parseInt(binImageBlue[i],2);
                rgbDataNew[i] = (alpha[i]<<24) | (redNew[i]<<16) | (greenNew[i]<<8) | (blueNew[i]);
    
               if(rgbDataNew[i] <= 0x670000){
    				rgbDataNew[i] = 0x000000;
    			}else{
    				rgbDataNew[i] = 0xffffff;
                }
            } //we have the new rgbData[]
    now, i can get the new image with BLACK n WHITE because i use threshold. It means that the newRgbData has changed, and it creates the new image.
    How can this happen? Why my first code cannot create the new image?
    please help me..

    regards,
    tbpjap

  14. #14
    Nokia Developer Moderator
    Join Date
    Dec 2011
    Posts
    174

    Re: [ASK] how to save image file

    The problem is not when you are converting or creating the image. So it must be when your are trying to modify your data. Check your replaceCharAt method and why it's not changing the right values.

    -tiviinik

  15. #15
    Registered User
    Join Date
    Mar 2012
    Posts
    17

    Re: [ASK] how to save image file

    thanks for your reply tiviinik and sorry for bothering you again..
    i have check my method, and i also check the value of binRGB using System.out.println().
    It shows me the right values.

    here is my method:
    Code:
    public static String replaceCharAt(String s, int pos, char c) {
            return s.substring(0, pos) + c + s.substring(pos + 1);
        }
    i don't know what's wrong with my code. Please help me again..

    Regards,
    tbpjap

Similar Threads

  1. how to save a image(captured from camera) to a file
    By sabera_mina in forum Symbian Media (Closed)
    Replies: 8
    Last Post: 2010-01-15, 09:23
  2. how to save an image
    By vineetbillorey in forum Mobile Java General
    Replies: 6
    Last Post: 2009-02-25, 06:44
  3. How to save Image to a file
    By selenatan in forum Mobile Java Media (Graphics & Sounds)
    Replies: 3
    Last Post: 2007-08-01, 19:20
  4. how to save file & how to save settings?
    By MGvision in forum Mobile Java General
    Replies: 2
    Last Post: 2007-06-13, 13:56
  5. large image file to save space?
    By fmx in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2003-07-02, 11:56

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •