如何在J2ME中编辑后保存图像将其写入文件夹

时间:2014-05-29 01:17:40

标签: java java-me j2mepolish

请帮我解决问题。非常感谢:)

public Image ResizeImage(Image image,int resizedimageWidth,intresizedHeight){
    int[] in = null;
    int[] out = null;
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    in = new int[imageWidth];
    int dy, dx;
    out = new int[resizedimageWidth * resizedHeight];
    for (int y = 0; y < resizedHeight; y++) {
        dy = y * imageHeight / resizedHeight;
        image.getRGB(in, 0, imageWidth, 0, dy, imageWidth, 1);
        for (int x = 0; x < resizedimageWidth; x++) {
            dx = x * imageWidth / resizedimageWidth;
        out[(resizedimageWidth * y) + x] = in[dx];
        }
    }
    Image resized = Image.createRGBImage(out, resizedimageWidth, resizedHeight, true);
            saveResizedImage(resized.toString().getBytes());
    return resized;
         }

private void saveResizedImage(byte[] bytes) {
    try {
    FileConnection fileConnection = (FileConnection) Connector.open(Utility.getMigoMobileDir() + StringConstants.USERS + StringConstants.SEPERATOR
                + userID + StringConstants.POSTFIX_PNG, Connector.READ_WRITE);
        if (fileConnection.exists()) {
            fileConnection.delete();
        }
        fileConnection.create();
        OutputStream oS = fileConnection.openOutputStream();
        oS.write(bytes);            
        oS.flush();     
        oS.close();         
        fileConnection.close();
        fileConnection = null;
        oS = null;
    } catch (IOException e) {
        e.printStackTrace();
        Utility.exception("[capture] " + e.toString());
    } catch (Exception e) {
        Utility.exception("[capture] " + e.toString());
    }
}

以上代码用于编辑(ResizeImage)和保存(saveResizedImage)图像文件夹,但输出为空图片。

1 个答案:

答案 0 :(得分:0)

你的问题是以下一行

        saveResizedImage(resized.toString().getBytes());

对象的toString只是一个无意义的表示而不是实际数据,你应该找到一种方法将调整大小后的图像的int []转换为byte []并保存它。