ranjith
ranjith

Reputation: 99

In j2me How to Save Image in Phone Memory for s40?

Images are locally saved in that application.I want save image from j2me application to phone memory.Is there is any encoder or convert byte array?How to Save it?Pls help me....

        try {


        String url=System.getProperty("fileconn.dir.photos")+"model0_0.jpg";

        FileConnection fc=(FileConnection)Connector.open(url,Connector.READ_WRITE);
        if(!fc.exists()) {

            fc.create();
        }else {
            //  return;
        }


        OutputStream os=fc.openOutputStream();
        int iw=galleryImage.getWidth();int ih=galleryImage.getHeight();
        rawInt=new int[iw*ih];
        galleryImage.getRGB(rawInt,0,iw,0,0,iw,ih);
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        for(int i=0;i<rawInt.length;i++)
            baos.write(rawInt[i]);
        byte byteData[]=baos.toByteArray();
        baos.close();
        ByteArrayInputStream b_stream=new ByteArrayInputStream(byteData);
        int i=0;
        /*while((i=b_stream.read())!=-1) {
            os.write(i);
        }*/

        for( i=0;i<content.length;i++) {
            os.write(b_stream.read());
        }

        //os.write(byteData);
        os.flush();
        os.close();
        System.out.println("\n\nImage Copied..\n");

        fc.close();

    } catch (IOException e) {
        //System.out.println("image not read for gallery");
        e.printStackTrace();
    }
    catch(java.lang.IllegalArgumentException iae){iae.printStackTrace();}
    catch(Exception e){e.printStackTrace();}

i tried this code.When one Unformatted file are stored in defaut image folder.That file size is 0.0KB.I think, image is not read............

Upvotes: 2

Views: 2178

Answers (2)

Mats Kruger
Mats Kruger

Reputation: 21

JPGEncoder is a really nice piece of SW and it works well on resource-constrained devices. However, it's based on Sun's JIMI library which is now owned by Oracle. Oracle's license terms are somewhat permissive, but they deny usage on embedded devices such as mobile phones. Depending on your situation, this might be a showstopper.

Upvotes: 2

hasanghaforian
hasanghaforian

Reputation: 14042

If you have an RGBA data from image,you would to encode it before save it,So find a suitable encoder for your purpose.You can use png format for all j2me program.First you would to earn RGBA data from image:

/**
   * Gets the channels of the image passed as parameter.
   * @param img Image
   * @return matrix of byte array representing the channels:
   * [0] --> alpha channel
   * [1] --> red channel
   * [2] --> green channel
   * [3] --> blue channel
   */

public byte[][] convertIntArrayToByteArrays(Image img) {
int[] pixels = new int[img.getWidth() * img.getHeight()];
img.getRGB(pixels, 0, img.getWidth(), 0, 0, img.getWidth(), 
           img.getHeight());

// separate channels
byte[] red = new byte[pixels.length];
byte[] green = new byte[pixels.length];
byte[] blue = new byte[pixels.length];
byte[] alpha = new byte[pixels.length];

for (int i = 0; i < pixels.length; i++) {
  int argb = pixels[i];
  //binary operations to separate the channels
  //alpha is the left most byte of the int (0xAARRGGBB)
  alpha[i] = (byte) (argb >> 24);
  red[i] = (byte) (argb >> 16);
  green[i] = (byte) (argb >> 8);
  blue[i] = (byte) (argb);
}

return new byte[][]{alpha, red, green, blue};
}   

Now download PNG.java class from here.In this class we have:

toPNG(int,int,byte[],byte[],byte[],byte[])

The first ints are the width and height of the image, the byte arrays are in order: alpha, red, green and... blue.The width and height are pretty straightforward:+getWidth():int and +getHeight():int from the Image object(as you done)and others are earned by ** convertIntArrayToByteArrays**:

byte[][] rgba = convertIntArrayToByteArrays(galleryImage);
byte[] encodeImage = toPNG(galleryImage.getWidth(),galleryImage.getHeight(),rgba[0] ,rgba[1] ,rgba[2],rgba[3]);   

Now you can save encodeImage in file by fileconnection.
If you like save image with format jpeg,download com.encoder.jpg from here,then:

import com.encoder.jpg.*;

//your input image
Image image = Image.createImage(128, 128);

JPGEncoder encoder = new JPGEncoder();
int quality = 65;
byte[] encodedImage = encoder.encode(image, quality);

//now save or send encoded jpeg image   

Finally you can use MediaProcessor from jsr234:

//Create MediaProcessor for raw Image
MediaProcessor mediaProc = GlobalManager.createMediaProcessor("image/raw");
//Get control over the format
ImageFormatControl formatControl = (ImageFormatControl)
        mediaProc.getControl("javax.microedition.amms.control.ImageFormatControl");
//Set necessary format
formatControl.setFormat("image/jpeg");   

Refrences:
stackoverflow
java-n-me

Upvotes: 1

Related Questions