participate


CLDC and MIDP - Image Encoding
This question is answered.

<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
primrose
Posts:70
Registered: 11/27/06
Image Encoding   
Oct 28, 2007 11:04 PM
 
 
Hi

I have an xml file and I read my data from it, but my question is:
how can I encode the image and then write it into this file? since I want to let my application read the encoded image from the xml file and then store it to Record Store.

Please any help will be so appreciated :(

thanks in advanced
 
deepspace
Posts:2,943
Registered: 2/2/05
Re: Image Encoding   
Oct 29, 2007 4:57 AM (reply 1 of 9)  (In reply to original post )
Helpful
 
Since xml is text and not binary, you'll could use a simple hex encoding, of use base64. The last one is a bit more efficient, but a bit harder to implement.
 
primrose
Posts:70
Registered: 11/27/06
Re: Image Encoding   
Oct 29, 2007 6:33 PM (reply 2 of 9)  (In reply to #1 )
 
 
thanks a lot dear

i tested using base64 encoding and it's works fine in emulator but in my mobile it's hanging!!

so what do think think the problem come from??
 
DarrylBurke
Posts:16,744
Registered: 7/2/07
Re: Image Encoding   
Oct 29, 2007 7:57 PM (reply 3 of 9)  (In reply to #2 )
 
 
You probably need to perform blocking IO in a separate Thread.

db
 
primrose
Posts:70
Registered: 11/27/06
Re: Image Encoding   
Oct 30, 2007 1:33 AM (reply 4 of 9)  (In reply to #3 )
 
 
thanks Darryl

but kindly, what do you mean about "blocking IO in a separate Thread"? and how can I do that?

please explain me more

thanks in advance

Edited by: primrose on Oct 30, 2007 1:33 AM
 
primrose
Posts:70
Registered: 11/27/06
Re: Image Encoding   
Oct 30, 2007 6:28 AM (reply 5 of 9)  (In reply to #4 )
 
 
I used this code for decoding base64:

ImageAsBase64 = "A94jsgbZ39HapwVNDOveOO8hdo3.......";
 
byte[] b =  new Base64Encoding().fromBase64(ImageAsBase64 );   //I couldn't use "Base64()" function, since I have an error "Base64 has private access in com.sun.midp.io"
        
MyImage = MayImage.createImage(b,0,b.length);

this code works fine in emulator but in my mobile doesn't

Can Any Body Help Me :(

thanks in advance

Edited by: primrose on Oct 30, 2007 6:28 AM
 
deepspace
Posts:2,943
Registered: 2/2/05
Re: Image Encoding   
Oct 30, 2007 6:59 AM (reply 6 of 9)  (In reply to #5 )
 
 
Where did you get the base64 decoding code from?

I use this one:

public class Base64 {
 
   private  char[]  map1  = new char[64];
   private  byte[]  map2  = new byte[128];
 
 
   /**  Constructor for the Base64 object */
   public Base64() {
      int  i  = 0;
      for ( char c = 'A'; c <= 'Z'; c++ ) {
         map1[i++] = c;
      }
      for ( char c = 'a'; c <= 'z'; c++ ) {
         map1[i++] = c;
      }
      for ( char c = '0'; c <= '9'; c++ ) {
         map1[i++] = c;
      }
      map1[i++] = '+';
      map1[i++] = '/';
      for ( i = 0; i < map2.length; i++ ) {
         map2[i] = -1;
      }
      for ( i = 0; i < 64; i++ ) {
         map2[map1[i]] = (byte) i;
      }
   }
 
   public String base64Encode( byte[] in ) {
      return base64Encode( in, 0, in.length );
   }
 
   public String base64Encode( byte[] in, int ip, int iLen ) {
      int     oDataLen  = ( iLen * 4 + 2 ) / 3;// output length without padding
      int     oLen      = ( ( iLen + 2 ) / 3 ) * 4;// output length including padding
      char[]  out       = new char[oLen];
      int     op        = 0;
      int     i0;
      int     i1;
      int     i2;
      int     o0;
      int     o1;
      int     o2;
      int     o3;
      while ( ip < iLen ) {
         i0 = in[ip++] & 0xff;
         i1 = ip < iLen ? in[ip++] & 0xff : 0;
         i2 = ip < iLen ? in[ip++] & 0xff : 0;
         o0 = i0 >>> 2;
         o1 = ( ( i0 & 3 ) << 4 ) | ( i1 >>> 4 );
         o2 = ( ( i1 & 0xf ) << 2 ) | ( i2 >>> 6 );
         o3 = i2 & 0x3F;
         out[op++] = map1[o0];
         out[op++] = map1[o1];
         out[op] = op < oDataLen ? map1[o2] : '=';
         op++;
         out[op] = op < oDataLen ? map1[o3] : '=';
         op++;
      }
      return new String( out );
   }
 
 
   public byte[] base64Decode( String i ) {
      char[]  in    = i.toCharArray();
      int     iLen  = in.length;
 
      if ( iLen % 4 != 0 ) {
         throw new IllegalArgumentException();
      }
      while ( iLen > 0 && in[iLen - 1] == '=' ) {
         iLen--;
      }
      int     oLen  = ( iLen * 3 ) / 4;
      byte[]  out   = new byte[oLen];
      int     ip    = 0;
      int     op    = 0;
      int     i0;
      int     i1;
      int     i2;
      int     i3;
      int     b0;
      int     b1;
      int     b2;
      int     b3;
      int     o0;
      int     o1;
      int     o2;
      while ( ip < iLen ) {
         i0 = in[ip++];
         i1 = in[ip++];
         i2 = ip < iLen ? in[ip++] : 'A';
         i3 = ip < iLen ? in[ip++] : 'A';
         if ( i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127 ) {
            throw new IllegalArgumentException();
         }
         b0 = map2[i0];
         b1 = map2[i1];
         b2 = map2[i2];
         b3 = map2[i3];
         if ( b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0 ) {
            throw new IllegalArgumentException();
         }
         o0 = ( b0 << 2 ) | ( b1 >>> 4 );
         o1 = ( ( b1 & 0xf ) << 4 ) | ( b2 >>> 2 );
         o2 = ( ( b2 & 3 ) << 6 ) | b3;
         out[op++] = (byte) o0;
         if ( op < oLen ) {
            out[op++] = (byte) o1;
         }
         if ( op < oLen ) {
            out[op++] = (byte) o2;
         }
      }
      return out;
   }
}


Should work fine?
 
primrose
Posts:70
Registered: 11/27/06
Re: Image Encoding   
Oct 30, 2007 8:53 AM (reply 7 of 9)  (In reply to #6 )
 
 
the Base64 encoding that i used is already existing in j2me. I tested your class but still I have the same error "Base64 has private access in com.sun.midp.io.Base64" so I changed its name from Base64 to for example Base46 but also have error :(

Running with storage root temp.DefaultColorPhone1193759499234
java.lang.IllegalArgumentException
        _at Base46.Base46Decode(Base46.java:82)_


any suggestions?

Edited by: primrose on Oct 30, 2007 8:52 AM
 
deepspace
Posts:2,943
Registered: 2/2/05
Re: Image Encoding   
Oct 31, 2007 1:51 AM (reply 8 of 9)  (In reply to #7 )
 
 
any suggestions?

Use my code...
 
primrose
Posts:70
Registered: 11/27/06
Re: Image Encoding   
Nov 2, 2007 5:06 AM (reply 9 of 9)  (In reply to #8 )
 
 
your code didn't work with me, but many thanks for your helping.

I searched for another code and i found this code and it's work fine :)


/* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany*/
 
package Files;
 
import java.io.*;
 
public class Base64 {
 
    static final char[] charTab =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
            .toCharArray();
 
    public static String encode(byte[] data) {
        return encode(data, 0, data.length, null).toString();
    }
 
    /** Encodes the part of the given byte array denoted by start and
    len to the Base64 format.  The encoded data is appended to the
    given StringBuffer. If no StringBuffer is given, a new one is
    created automatically. The StringBuffer is the return value of
    this method. */
 
    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 =
                ((((int) data[i]) & 0x0ff) << 16)
                    | ((((int) data[i + 1]) & 0x0ff) << 8)
                    | (((int) 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 =
                ((((int) data[i]) & 0x0ff) << 16)
                    | ((((int) 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 = (((int) data[i]) & 0x0ff) << 16;
 
            buf.append(charTab[(d >> 18) & 63]);
            buf.append(charTab[(d >> 12) & 63]);
            buf.append("==");
        }
 
        return buf;
    }
 
    static int decode(char c) {
 
        if (c >= 'A' && c <= 'Z')
            return ((int) c) - 65;
        else if (c >= 'a' && c <= 'z')
            return ((int) c) - 97 + 26;
        else if (c >= '0' && c <= '9')
            return ((int) c) - 48 + 26 + 26;
        else
            switch (c) {
                case '+' :
                    return 62;
                case '/' :
                    return 63;
                case '=' :
                    return 0;
                default :
                    throw new RuntimeException(
                        "unexpected code: " + c);
            }
    }
 
    /** Decodes the given Base64 encoded String to a new byte array.
    The byte array holding the decoded data is returned. */
 
    public static byte[] decode(String s) {
 
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
 
            decode(s, bos);
 
        }
        catch (IOException e) {
            throw new RuntimeException();
        }
        return bos.toByteArray();
    }
 
    public static void decode(String s, OutputStream os)
        throws IOException {
        int i = 0;
 
        int len = s.length();
 
        while (true) {
            while (i < len && s.charAt(i) <= ' ')
                i++;
 
            if (i == len)
                break;
 
            int tri =
                (decode(s.charAt(i)) << 18)
                    + (decode(s.charAt(i + 1)) << 12)
                    + (decode(s.charAt(i + 2)) << 6)
                    + (decode(s.charAt(i + 3)));
 
 
            os.write((tri >> 16) & 255);
            if (s.charAt(i + 2) == '=')
                break;
            os.write((tri >> 8) & 255);
            if (s.charAt(i + 3) == '=')
                break;
            os.write(tri & 255);
 
            i += 4;
        }
    }
}
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 25
  • Guests : 132

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums