J2ME:Downloading an Image from server and saving on Mobile
I'm trying to download an image from server and save the image on mobile phone.I'm able to download the image but I need to resize the image and then store the image to mobile.
the code is like this.
public void requestPlatform(String strURL) throws IOException {
try {
System.out.println("requestPlatform");
byte [] imageBytes = null;
if ((im = getImage1(strURL)) != null) {
im=resizeImage(im, width, height);
if(im!=null)
{
System.out.println("Image");
}
else
System.out.println("NoImage");
imageBytes=get_Byte_Array(im);
fileCon=(FileConnection)Connector.open("file:///root1/photos/diwali1.jpg",Connector.READ_WRITE);
开发者_JS百科 fileCon.create();
if(fileCon.exists())
{
System.out.println("File created");
}
else
{
System.out.println("File not created");
}
out = fileCon.openDataOutputStream();
if(imageBytes!=null)
{
out.write(imageBytes, 0, imageBytes.length);
}
if(out!=null)
{
out.close();
}
if(fileCon!=null)
{
fileCon.close();
}
im2=Image.createImage(imageBytes, 0, imageBytes.length);
canvas.repaint();
}
} catch (Exception e) {
System.err.println("Msg: " + e.toString());
}
}
public byte[] get_Byte_Array(Image img) {
int[] imgRgbData = new int[img.getWidth() * img.getHeight()];
byte[] imageData2 = null;
try
{
img.getRGB( imgRgbData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight() );
} catch( Exception e )
{
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream( baos );
try{
for( int i = 0; i < imgRgbData.length; i++ )
{
dos.writeInt( imgRgbData[i] );
}
imageData2 = baos.toByteArray();
baos.close();
dos.close();
}catch(Exception e)
{
}
return imageData2;
}
The problem is when I'm trying to resize the image and then save it on phone it just creates a file but there is no image.So i think there is some mistake while i'm converting image to byteArray.
The best way to transform image is preparing it on server side. But if you aren't able to do this create image from byte data and after that use method
Image.createImage(Image image, int x, int y, int width, int height, int **transform**)
It's very expensive in resources (memory and cpu utilization) but the easiest way.
About saving. You can save data into RMS (this is possible on all J2ME mobiles) or to file system (only on devices with JSR-75 FileConnection API).
you can use this to resize your image, although it is better to resize it on the server instead of doing this on the phone. you need to adapt it to resize the image to a specified width and height.
private Image resizeImage(Image src) {
int srcWidth = src.getWidth();
int srcHeight = src.getHeight();
Image tmp = Image.createImage(screenWidth, srcHeight);
Graphics g = tmp.getGraphics();
int ratio = (srcWidth << 16) / screenWidth;
int pos = ratio/2;
//Horizontal Resize
for (int x = 0; x < screenWidth; x++) {
g.setClip(x, 0, 1, srcHeight);
g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
Image resizedImage = Image.createImage(screenWidth, screenHeight);
g = resizedImage.getGraphics();
ratio = (srcHeight << 16) / screenHeight;
pos = ratio/2;
//Vertical resize
for (int y = 0; y < screenHeight; y++) {
g.setClip(0, y, screenWidth, 1);
g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
pos += ratio;
}
return resizedImage;
}//resize image
this code is taken from here
This is a great Image resizing script that I use. Hope it serves your needs.
public Image ResizeImage(Image image, int resizedWidth, int resizedHeight) {
int[] in = null, out = null;
int width = image.getWidth(), height = image.getHeight();
in = new int[width];
int i=0;
int dy,dx;
out = new int[resizedWidth * resizedHeight];
for (int y = 0; y < resizedHeight; y++)
{
dy = y * height / resizedHeight;
image.getRGB(in,0,width,0,dy,width,1);
for (int x = 0; x < resizedWidth; x++)
{
dx = x * width / resizedWidth;
out[(resizedWidth * y) + x] = in[dx];
}
}
Image resized = Image.createRGBImage(out,resizedWidth,resizedHeight,true);
return resized;
}
精彩评论