将int []转换为字节数组,将byte数组转换为image

时间:2013-01-03 21:15:50

标签: java image

尝试存储到图像。 bt是字节数组

InputStream is = new ByteArrayInputStream(bt);
Image img = ImageIO.read(is);
ImageIO.write(img,"jpg",new File("C:\\home_loan.jpg") );

,请帮帮我,

错误 - :找不到符号

symbol:方法write(java.awt.Image,java.lang.String,java.io.File)

location:class javax.imageio.ImageIO

ImageIO.write(img,“jpg”,new File(“C:\ home_loan.jpg”));

   ^  

2 个答案:

答案 0 :(得分:0)

使用BufferedImage。这是一个由内存支持的Image,有两个setRGB方法。

按此定制您的数据;否则你需要转换数据结构,在一个BufferedImage上工作。

答案 1 :(得分:0)

您可以使用以下方法 -

/**
 * @param path full path of image
 * @return image file for supplied path
 */
private BufferedImage getImage(String path) {
    BufferedImage image = null;
    File file = new File(path);
    try {
        image = ImageIO.read(file);
    } catch(Exception e) {
        e.printStackTrace();
    }
    return image;
}

/**
 * @param userSpaceImage 
 * @return byte array of supplied image
 */
private byte[] getByteData(BufferedImage userSpaceImage) {
    WritableRaster raster = userSpaceImage.getRaster();
    DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
    return buffer.getData();
}

/**
 * @param image image-file to save
 * @param file to save image to
 * @param extension of image file
 * @return full path of saved image
 */
private String saveImage(BufferedImage image, File file, String extension) {
    try {
        ImageIO.write(image, extension, file);
    } catch(Exception e) {
        e.printStackTrace();
    }
    return file.getAbsolutePath();
}