×

Discussion Board

Results 1 to 10 of 10
  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    15

    store image in recordstore

    Hi,

    I try to create an application with recordstore that can save image. Can recordstore save image? I found that recordstore can store byte data, i guess i have to convert the image to byte data before store it in recordstore. how to convert image (example .png image) to byte?

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2004
    Location
    The Netherlands
    Posts
    17
    Indeed, you can only store bytes into the RecordStore.
    So you have to convert your image to bytes.
    To give you an idea of how I solved this:
    General idea was:
    - store width and height of image into record
    - store every pixel as 3 bytes into record

    Code:
    // your image
    Image image = Image.createImage("/image.jpg");
    
    // get width and height
    int width=image.getWidth();
    int height=image.getHeight();
    
    // create pixel buffer to hold the pixels of the image
    // 1 pixel = 1 int, but we can't store ints...
    int pixelData[]=new int[width*height];
    
    // so we create a bytebuffer which we can write to the RecordStore
    // 1 pixel = 3 bytes (or 4 when you use alpha channel)
    // we add 8 more bytes to store the width and height
    imageData=new byte[width*height*3+8];
    
    // put the width and height into the first 8 bytes
    for(int b=0;b<4;b++) {
      byte widthByte=(byte)(width>>>b*8&0xff);
      imageData[b]=widthByte;
      byte heightByte=(byte)(height>>>b*8&0xff);
      imageData[b+4]=heightByte;
    }
    
    // get pixeldata from image
    image.getRGB(pixelData,0,width,0,0,width,height);
    
    // get red, green and blue byte from every pixel
    for(int p=0;p<pixelData.length;p++) {
      imageData[p*3+8]=(byte)(pixelData[p]>>16); // red byte
      imageData[p*3+8+1]=(byte)(pixelData[p]>>8); // green byte
      imageData[p*3+8+2]=(byte)pixelData[p]; // blue byte
    }
    
    // now you can write the image byte data (imageData) to your RecordStore
    // for getting your image back from the RecordStore, you should reverse this procedure

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    6

    Re: store image in recordstore

    how to retrive the data from this type ?????????

  4. #4
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: store image in recordstore

    Quote Originally Posted by abhisardeshmukh View Post
    how to retrive the data from this type ?????????
    Hi,
    Not exactly clear what you are talking about..I guess you are asking that how to draw the image from the byte array..correct,
    if yes then you can follow like this -

    Image.createImage(byteArray,0,byteArray.length);
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  5. #5
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: store image in recordstore

    Hi,
    In order to save the image and load the image you can just check the code here,
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.rms.*;
    import java.io.*;

    public class ImageStore extends MIDlet implements CommandListener {

    private Command CmdExit;
    private Command CmdOpen;
    private Command CmdBack;
    private Command CmdSave;
    private Display display;
    RecordStore rStore;
    Form form = null;
    Image image = null;
    InputStream is =null;

    public ImageStore() {

    rStore = null;

    display = Display.getDisplay(this);

    CmdExit = new Command("Exit", 1, 2);
    CmdOpen = new Command("Show", 1, 3);
    CmdBack = new Command("Back", 1, 3);
    CmdSave = new Command("Save", 1, 3);

    form = new Form("Image Show");

    }

    public void startApp() {
    try {
    rStore = RecordStore.openRecordStore("imagefile", true);
    } catch(RecordStoreException recordstoreexception) {
    recordstoreexception.printStackTrace();
    }
    try {

    is = getClass().getResourceAsStream("/leaf.jpg");
    image = Image.createImage(is);
    form.append(image);

    } catch(IOException ioexception) { }
    form.addCommand(CmdExit);
    form.addCommand(CmdSave);
    form.addCommand(CmdOpen);
    form.setCommandListener(this);
    display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void Close() {
    try {
    rStore.closeRecordStore();
    } catch(RecordStoreNotOpenException recordstorenotopenexception) {
    recordstorenotopenexception.printStackTrace();
    } catch(RecordStoreException recordstoreexception) {
    recordstoreexception.printStackTrace();
    }
    }

    public void destroyApp(boolean flag) {
    Close();
    }

    public Image load(int width,int height) {

    byte[] b = null;
    String imagename = null;
    Image image = null;

    try {

    int i = rStore.getNumRecords();

    for(int j = 1; j < i + 1; j++) {

    if(rStore.getRecord(j) != null) {

    b = rStore.getRecord(j);
    ByteArrayInputStream bin =
    new ByteArrayInputStream( b );

    DataInputStream din = new DataInputStream( bin );

    imagename = din.readUTF();
    int remaining =
    (b.length-imagename.getBytes().length-2)/4;

    int[] rawdata = new int[remaining];

    for(int k =0 ;k<rawdata.length ;k++) {
    rawdata[k] = din.readInt();
    }

    image = Image.createRGBImage(rawdata,
    width, height, false);

    bin.reset();
    din.close();
    din =null;
    }
    }
    } catch (IOException e) {

    e.printStackTrace();

    } catch(RecordStoreException recordstoreexception) {

    recordstoreexception.printStackTrace();

    }

    return image;
    }

    public boolean save(Image img, int width,
    int height, String imgName) {

    if (img == null || width < 0 || height < 0 || imgName == null) {

    throw new IllegalArgumentException("Check arguments");

    }

    int[] imgRgbData = new int[width * height];

    try {

    img.getRGB(imgRgbData, 0, width, 0, 0, width, height);

    } catch (Exception e) {
    // Problem getting image RGB data
    return false;
    }
    try {
    // Write image data to output stream (in order to get
    // the record bytes in needed form)
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeUTF(imgName);

    for (int i = 0; i < imgRgbData.length; i++) {
    dos.writeInt(imgRgbData[i]);
    }

    // Open record store, create if it doesn't exist
    rStore.addRecord(baos.toByteArray(), 0,
    baos.toByteArray().length); // Add record

    } catch (RecordStoreNotFoundException rsnfe) {
    // Record storage not found
    return false;
    } catch (RecordStoreException rse) {
    // Other record storage problem
    return false;
    } catch (IOException ioe) {
    // Problem writing data
    return false;
    }

    return true; // We've successfuly done
    }

    public void commandAction(Command command, Displayable displayable) {

    if(command == CmdExit) {

    destroyApp(true);
    notifyDestroyed();

    }
    else if(command == CmdOpen) {

    Form showform = new Form("Image from DB");
    Image i = load(image.getWidth(),image.getHeight());

    if(i !=null ) {

    Image img = Image.createImage(i);
    showform.append(img);

    }

    showform.addCommand(CmdBack);
    showform.setCommandListener(this);
    display.setCurrent(showform);

    } else if(command == CmdBack) {

    display.setCurrent(form);

    } else if(command == CmdSave) {

    byte[] b = null;
    Alert a =new Alert("Image saved");

    try {
    if(save(image,image.getWidth(),image.getHeight(),"leaf"))
    a.setString("Success");
    else
    a.setString("Failed");
    a.setTimeout(1000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    display.setCurrent(a);
    }
    }
    }
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    6

    Question Re: store image in recordstore

    I want to save the image in to the RecordStore
    and also get it back from recordstore




    Quote Originally Posted by mkleijer View Post
    Indeed, you can only store bytes into the RecordStore.
    So you have to convert your image to bytes.
    To give you an idea of how I solved this:
    General idea was:
    - store width and height of image into record
    - store every pixel as 3 bytes into record

    Code:
    // your image
    Image image = Image.createImage("/image.jpg");
    
    // get width and height
    int width=image.getWidth();
    int height=image.getHeight();
    
    // create pixel buffer to hold the pixels of the image
    // 1 pixel = 1 int, but we can't store ints...
    int pixelData[]=new int[width*height];
    
    // so we create a bytebuffer which we can write to the RecordStore
    // 1 pixel = 3 bytes (or 4 when you use alpha channel)
    // we add 8 more bytes to store the width and height
    imageData=new byte[width*height*3+8];
    
    // put the width and height into the first 8 bytes
    for(int b=0;b<4;b++) {
      byte widthByte=(byte)(width>>>b*8&0xff);
      imageData[b]=widthByte;
      byte heightByte=(byte)(height>>>b*8&0xff);
      imageData[b+4]=heightByte;
    }
    
    // get pixeldata from image
    image.getRGB(pixelData,0,width,0,0,width,height);
    
    // get red, green and blue byte from every pixel
    for(int p=0;p<pixelData.length;p++) {
      imageData[p*3+8]=(byte)(pixelData[p]>>16); // red byte
      imageData[p*3+8+1]=(byte)(pixelData[p]>>8); // green byte
      imageData[p*3+8+2]=(byte)pixelData[p]; // blue byte
    }
    
    // now you can write the image byte data (imageData) to your RecordStore
    // for getting your image back from the RecordStore, you should reverse this procedure
    Quote Originally Posted by raj_J2ME View Post
    Hi,
    Not exactly clear what you are talking about..I guess you are asking that how to draw the image from the byte array..correct,
    if yes then you can follow like this -


    Thanks,

  7. #7
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: store image in recordstore

    Hi,
    Did you check the last post..?
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    6

    Smile Re: store image in recordstore

    Thanx dude for ur valuable reply against my que......



    Quote Originally Posted by raj_J2ME View Post
    Hi,
    In order to save the image and load the image you can just check the code here,
    import javax.microedition.lcdui.*;
    import javax.microedition.midlet.MIDlet;
    import javax.microedition.rms.*;
    import java.io.*;

    public class ImageStore extends MIDlet implements CommandListener {

    private Command CmdExit;
    private Command CmdOpen;
    private Command CmdBack;
    private Command CmdSave;
    private Display display;
    RecordStore rStore;
    Form form = null;
    Image image = null;
    InputStream is =null;

    public ImageStore() {

    rStore = null;

    display = Display.getDisplay(this);

    CmdExit = new Command("Exit", 1, 2);
    CmdOpen = new Command("Show", 1, 3);
    CmdBack = new Command("Back", 1, 3);
    CmdSave = new Command("Save", 1, 3);

    form = new Form("Image Show");

    }

    public void startApp() {
    try {
    rStore = RecordStore.openRecordStore("imagefile", true);
    } catch(RecordStoreException recordstoreexception) {
    recordstoreexception.printStackTrace();
    }
    try {

    is = getClass().getResourceAsStream("/leaf.jpg");
    image = Image.createImage(is);
    form.append(image);

    } catch(IOException ioexception) { }
    form.addCommand(CmdExit);
    form.addCommand(CmdSave);
    form.addCommand(CmdOpen);
    form.setCommandListener(this);
    display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void Close() {
    try {
    rStore.closeRecordStore();
    } catch(RecordStoreNotOpenException recordstorenotopenexception) {
    recordstorenotopenexception.printStackTrace();
    } catch(RecordStoreException recordstoreexception) {
    recordstoreexception.printStackTrace();
    }
    }

    public void destroyApp(boolean flag) {
    Close();
    }

    public Image load(int width,int height) {

    byte[] b = null;
    String imagename = null;
    Image image = null;

    try {

    int i = rStore.getNumRecords();

    for(int j = 1; j < i + 1; j++) {

    if(rStore.getRecord(j) != null) {

    b = rStore.getRecord(j);
    ByteArrayInputStream bin =
    new ByteArrayInputStream( b );

    DataInputStream din = new DataInputStream( bin );

    imagename = din.readUTF();
    int remaining =
    (b.length-imagename.getBytes().length-2)/4;

    int[] rawdata = new int[remaining];

    for(int k =0 ;k<rawdata.length ;k++) {
    rawdata[k] = din.readInt();
    }

    image = Image.createRGBImage(rawdata,
    width, height, false);

    bin.reset();
    din.close();
    din =null;
    }
    }
    } catch (IOException e) {

    e.printStackTrace();

    } catch(RecordStoreException recordstoreexception) {

    recordstoreexception.printStackTrace();

    }

    return image;
    }

    public boolean save(Image img, int width,
    int height, String imgName) {

    if (img == null || width < 0 || height < 0 || imgName == null) {

    throw new IllegalArgumentException("Check arguments");

    }

    int[] imgRgbData = new int[width * height];

    try {

    img.getRGB(imgRgbData, 0, width, 0, 0, width, height);

    } catch (Exception e) {
    // Problem getting image RGB data
    return false;
    }
    try {
    // Write image data to output stream (in order to get
    // the record bytes in needed form)
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeUTF(imgName);

    for (int i = 0; i < imgRgbData.length; i++) {
    dos.writeInt(imgRgbData[i]);
    }

    // Open record store, create if it doesn't exist
    rStore.addRecord(baos.toByteArray(), 0,
    baos.toByteArray().length); // Add record

    } catch (RecordStoreNotFoundException rsnfe) {
    // Record storage not found
    return false;
    } catch (RecordStoreException rse) {
    // Other record storage problem
    return false;
    } catch (IOException ioe) {
    // Problem writing data
    return false;
    }

    return true; // We've successfuly done
    }

    public void commandAction(Command command, Displayable displayable) {

    if(command == CmdExit) {

    destroyApp(true);
    notifyDestroyed();

    }
    else if(command == CmdOpen) {

    Form showform = new Form("Image from DB");
    Image i = load(image.getWidth(),image.getHeight());

    if(i !=null ) {

    Image img = Image.createImage(i);
    showform.append(img);

    }

    showform.addCommand(CmdBack);
    showform.setCommandListener(this);
    display.setCurrent(showform);

    } else if(command == CmdBack) {

    display.setCurrent(form);

    } else if(command == CmdSave) {

    byte[] b = null;
    Alert a =new Alert("Image saved");

    try {
    if(save(image,image.getWidth(),image.getHeight(),"leaf"))
    a.setString("Success");
    else
    a.setString("Failed");
    a.setTimeout(1000);
    } catch (Exception e) {
    e.printStackTrace();
    }
    display.setCurrent(a);
    }
    }
    }
    Thanks,

  9. #9
    Super Contributor
    Join Date
    Apr 2007
    Posts
    2,708

    Re: store image in recordstore

    try not to quote everything in ur posts, u'll create a lot of unnecessary long posts...
    just a hint, im assuming ur new to forums....

  10. #10
    Super Contributor
    Join Date
    Mar 2008
    Location
    The Capital of INDIA
    Posts
    4,328

    Thumbs up Re: store image in recordstore

    Hi,
    your welcome,
    It is nice to hear and sweet to see that you fix the issue,lines given to you were helpful,
    Thanks,
    Thanks with Regards,

    R a j - The K e r n e l


    Join Delhi-NCR Nokia Developer's Community,

Posting Permissions

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