J2ME:从服务器下载图像并在手机上保存

问题描述:

我试图从服务器下载图像并将图像保存到手机上。我可以下载图像,但我需要调整图像大小然后再存储图像移动。J2ME:从服务器下载图像并在手机上保存

代码是这样的。

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); 

     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; 
    } 

问题是,当我试图调整图像大小,然后将其保存在手机上,它只是创建了一个文件,但没有image.So我认为有一些错误,而我在转换图像字节组。

转换图像的最佳方式是在服务器端准备它。但是,如果你不能做到这一点从字节的数据创建图像和使用方法

Image.createImage(Image image, int x, int y, int width, int height, int **transform**) 

后,它在资源(内存和CPU利用率),但最简单的方法非常昂贵。

关于保存。您可以将数据保存到RMS(在所有J2ME手机上都可以)或文件系统(仅在具有JSR-75 FileConnection API的设备上)。

+0

您不能使用此方法调整图像大小,在JME中本机不支持调整图像大小。 – 2010-11-02 20:05:39

+0

是的,你是对的。 – Donz 2010-11-03 10:27:48

+0

所以你需要实现你自己的resize algorythm。请参阅http://developers.sun.com/mobility/reference/techart/design_guidelines/image_resizing.html或http://discussion.forum.nokia.com/forum/showthread.php?73221-How-to-resize-image -using-MIDP-2.0 – Donz 2010-11-03 10:31:07

您可以使用此功能调整图像大小,但最好在服务器上调整大小,而不是在手机上调整大小。您需要调整它以将图像调整为指定的宽度和高度。

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 

这个代码是从here

+0

嗨thanx很多它真的有帮助。但我遇到了一个问题,当我用FileConnection保存我的图像文件正在创建,但没有图像显示。如果任何人有任何想法plz会尽快回复 – 2010-11-03 12:14:27

+0

如果没有看到您的代码,很难确定问题出在哪里。它应该没有问题。图像是否被创建?你可以用手机上的任何原生应用程序打开它吗? – 2010-11-03 18:09:20

采取这是一个伟大的图片大小脚本,我使用。希望它能满足您的需求。

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; 
}