×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    12

    Image - byte[] - String (re)conversion

    Hi all. If I have a byte[] representing a PNG image, can I convert the byte[] into a String and then back to byte[] and Image? I've tried the code below but the second image gets very degradated.
    The conversion is Image->byte[]->String->byte[]->Image

    I am sure that the first byte[] is correct and represents the image correctly.

    Code:
    byte[] imgData; // bytes representing an image 
    //Create Image
    Image img = Image.createImage(imgData, 0, imgData.length);
    
    [SHOW IMG] // Showing img is OK here
    
    String s = new String(imgData); // create String from image bytes
    
    byte[] imgData2 = s.getBytes(); // get the bytes from String 
    
    // Create new Image
    Image img2 = Image.createImage(imgData2, 0, imgData2.length);
    
    [SHOW IMG2] // Showing img2 is not OK. The image gets degradated

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Re: Image - byte[] - String (re)conversion

    you can not just convert byte[] data to string. you should encode the byte array with the following base64 encoder. then you get a string representing the bytes.
    Code:
    public static StringBuffer encode(byte [] data, int start, int len, StringBuffer buf) {
            
            if (buf == null)
                buf = new StringBuffer(data.length * 3 / 2);
            
            int end = len - 3;
            int i = start;
            int n = 0;
            
            while (i <= end) {
                int d = ((( data [i]) & 0x0ff) << 16)
                | ((( data [i+1]) & 0x0ff) << 8)
                | (( data [i+2]) & 0x0ff);
                
                buf.append(charTab [(d >> 18) & 63]);
                buf.append(charTab [(d >> 12) & 63]);
                buf.append(charTab [(d >> 6) & 63]);
                buf.append(charTab [d & 63]);
                
                i += 3;
                
                if (n++ >= 14) {
                    n = 0;
                    buf.append("\r\n");
                }
            }
            
            
            if (i == start + len - 2) {
                int d = ((( data [i]) & 0x0ff) << 16)
                | ((( data [i+1]) & 255) << 8);
                
                buf.append(charTab [(d >> 18) & 63]);
                buf.append(charTab [(d >> 12) & 63]);
                buf.append(charTab [(d >> 6) & 63]);
                buf.append("=");
            } else if (i == start + len - 1) {
                int d = (( data [i]) & 0x0ff) << 16;
                
                buf.append(charTab [(d >> 18) & 63]);
                buf.append(charTab [(d >> 12) & 63]);
                buf.append("==");
            }
            
            return buf;
        }

Similar Threads

  1. J2ME encrypt C# decrypt
    By josual in forum Mobile Java Networking & Messaging & Security
    Replies: 13
    Last Post: 2008-10-20, 04:29
  2. Image attribute in Bluetooth ServiceRecord
    By lampz in forum Mobile Java Networking & Messaging & Security
    Replies: 7
    Last Post: 2008-02-18, 20:30
  3. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  4. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  5. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31

Posting Permissions

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