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

Java me J2ME:从服务器下载图像并保存在手机上,java-me,Java Me,我正在尝试从服务器下载图像并将其保存到手机上。我可以下载图像,但需要调整图像大小,然后将图像存储到手机上 代码是这样的 public void requestPlatform(String strURL) throws IOException { try { System.out.println("requestPlatform"); byte [] imageBytes = null; if ((im = getImage1(strURL)) != null)

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

代码是这样的

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;
    }
public void requestPlatform(字符串strURL)引发IOException{
试一试{
System.out.println(“请求平台”);
byte[]imageBytes=null;
如果((im=getImage1(strURL))!=null){
im=调整图像大小(im、宽度、高度);
如果(im!=null)
{
System.out.println(“图像”);
}
其他的
System.out.println(“NoImage”);
imageBytes=获取字节数组(im);
fileCon=(FileConnection)连接器。打开(“file:///root1/photos/diwali1.jpg,连接器。读写);
create();
如果(fileCon.exists())
{
System.out.println(“创建的文件”);
}
其他的
{
System.out.println(“未创建文件”);
}
out=fileCon.openDataOutputStream();
if(imageBytes!=null)
{
out.write(imageBytes,0,imageBytes.length);
}
if(out!=null)
{
out.close();
}
如果(fileCon!=null)
{
fileCon.close();
}
im2=Image.createImage(imageBytes,0,imageBytes.length);
canvas.repaint();
}
}捕获(例外e){
System.err.println(“Msg:+e.toString());
}
}
公共字节[]获取字节数组(图像img){
int[]imgRgbData=newint[img.getWidth()*img.getHeight()];
字节[]imageData2=null;
尝试
{
getRGB(imgRgbData,0,img.getWidth(),0,0,img.getWidth(),img.getHeight());
}捕获(例外e)
{
}
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
DataOutputStream dos=新的DataOutputStream(BAS);
试一试{
对于(int i=0;i

问题是,当我尝试调整图像大小,然后将其保存在手机上时,它只创建了一个文件,但没有图像。因此,我认为在将图像转换为byteArray时出现了一些错误。

转换图像的最佳方法是在服务器端进行准备。但若你们不能做到这一点,那个么就从字节数据创建图像,然后使用这个方法

Image.createImage(Image image, int x, int y, int width, int height, int **transform**)
这在资源(内存和cpu利用率)方面非常昂贵,但却是最简单的方法


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

您可以使用此选项调整图像大小,不过最好在服务器上调整图像大小,而不是在手机上调整图像大小。您需要对其进行调整,以将图像大小调整为指定的宽度和高度

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
private Image resizeImage(Image src){
int srcWidth=src.getWidth();
int srchheight=src.getHeight();
Image tmp=Image.createImage(屏幕宽度、高度);
Graphics g=tmp.getGraphics();
int ratio=(srcWidth>16),0,Graphics.LEFT | Graphics.TOP);
pos+=比率;
}
Image resizedImage=Image.createImage(屏幕宽度、屏幕高度);
g=resizedImage.getGraphics();
比率=(srchheight>16),Graphics.LEFT | Graphics.TOP);
pos+=比率;
}
返回resizedImage;
}//调整图像大小

这段代码取自

这是我使用的一个很棒的图像大小调整脚本。希望它能满足你的需要

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;   
}
public Image ResizeImage(Image Image,int resizedWidth,int resizedHeight){
int[]in=null,out=null;
int width=image.getWidth(),height=image.getHeight();
in=新的整数[宽度];
int i=0;
int-dy,dx;
out=新整数[resizedWidth*resizedHeight];
对于(int y=0;y
您不能使用此方法调整图像大小,JME本机不支持调整图像大小。因此,您需要实现自己的调整大小算法。看到或嘿thanx很多这真的很有帮助。但是我在使用文件连接保存图像时遇到了一个问题,文件正在创建,但没有图像可显示。如果有人有任何想法,请尽快回复。如果没有看到您的代码,很难判断问题出在哪里。它应该可以毫无问题地工作。图像是否已创建?你能用手机上的任何本机应用程序打开它吗?