×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Regular Contributor
    Join Date
    Sep 2005
    Posts
    138

    How to serialize an Image object

    Hello:

    I have a method to encode to base 64 an string:

    String s = Base64.encode(miObject.getBytes());

    I want to write in a XML document an Image, but i need to serialize it, and J2ME doen't support that.

    I've seen a few references to a sun's document talking about serialization but the link everytime is broken.

    How can I obtain the String i need? or the bytes?

    Thank you very much

  2. #2
    Super Contributor
    Join Date
    Mar 2005
    Location
    Paris
    Posts
    814

    Re: How to serialize an Image object

    rtfm : Image.getRBG()

  3. #3
    Regular Contributor
    Join Date
    Sep 2005
    Posts
    138

    Re: How to serialize an Image object

    Thank you njzk2, i will try it right now! in a few hours i'll post you my results

  4. #4
    Regular Contributor
    Join Date
    Sep 2005
    Posts
    138

    Re: How to serialize an Image object

    Quote Originally Posted by njzk2
    rtfm : Image.getRBG()

    Hello again.

    I can obtain an array of ints with getRGB, and I can transform that array into a String, and then write the string into an XML document:

    <Image> xxxxx </Image>

    But later, when I read the xml and recover my String. How I can create my Image object with thas String?

    Thank you again.

  5. #5
    Regular Contributor
    Join Date
    Sep 2005
    Posts
    138

    Re: How to serialize an Image object

    OK, finally i write this code, It works ok a few times yes, a few times no XD

    public String serializarImagen(Image img){

    //System.gc();

    int[] imgRgbData = new int[14700]; //Creamos un array de pixels del tamaño de la foto (es constante: 140*105)
    img.getRGB(imgRgbData, 0, 140, 0, 0, 140, 105); //Pasamos los pixels de la imagen al array
    byte[] misBytes = null;

    try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    for (int i = 0; i < (14699); i++) { //Escribimos en un array de bytes
    dos.writeInt(imgRgbData[i]); //Escribo el entero como 4 bytes
    }

    dos.flush(); //Los elementos que pudieran quedar "en medio" los termino de pasar al array en memoria

    misBytes = baos.toByteArray();

    //Vale, una vez tengo el vector de bytes, es sencillo codificarlo

    }catch (Exception e) {e.printStackTrace();}

    return (Base64.encode(misBytes)); //Esta cadena ya la podria empotrar en un XML
    }

    public Image desSerializarImagen(String str){

    byte[] misNuevosBytes = Base64.decode(str);
    int[] imgRgbData2 = new int[14700];

    try{
    ByteArrayInputStream bais = new ByteArrayInputStream(misNuevosBytes, 0, misNuevosBytes.length);
    DataInputStream dis = new DataInputStream(bais);


    for (int i = 0; i < (14699); i++) {
    imgRgbData2[i] = dis.readInt();
    }

    }catch (Exception e){e.printStackTrace();}

    return(Image.createRGBImage(imgRgbData2, 140, 105, true));
    }


    but sometimes I have a java.lang.OutOfMemoryError. Anybody knows why?
    Last edited by Pepper_91; 2005-12-30 at 16:08.

Similar Threads

  1. --- ???save image problem??? ---
    By ferenn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2007-10-01, 14:33
  2. HELP: Mutable Image to Immutable Image?
    By rj_cybersilver in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2005-03-26, 09:58
  3. intercept/catch the image object
    By Rx- in forum Bluetooth Technology
    Replies: 0
    Last Post: 2004-02-18, 06:25
  4. Game Developement - Display object on a image background
    By kenkwokkam in forum Mobile Java General
    Replies: 0
    Last Post: 2003-02-08, 14:58
  5. Get pixel data from an image object on Nokia phones?
    By inopia in forum Mobile Java General
    Replies: 1
    Last Post: 2002-05-15, 09:27

Posting Permissions

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