无法获得该方法的工作

时间:2013-08-14 07:46:36

标签: java

我这里有这个方法,它将图像转换为字节数组。

public byte[] imageToCompressedByteArray(Image image) throws IOException {
//load the image
String f = "C:\\Users\\mamed\\Documents\\NetBeansProjects\\Main\\src\\resources\\accept.png";
image = ImageIO.read(new FileInputStream(new File(f)));


// get image size
int width = image.getWidth(null);
int height = image.getHeight(null);


try {
  int[] imageSource = new int[width * height];
  PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, imageSource, 0, width);
  pg.grabPixels();


  ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
  GZIPOutputStream zippedStream = new GZIPOutputStream(byteStream);
  ObjectOutputStream objectStream = new ObjectOutputStream(zippedStream);
  objectStream.writeShort(width);
  objectStream.writeShort(height);
  objectStream.writeObject(imageSource);
  objectStream.flush();
  objectStream.close();
  return byteStream.toByteArray();
}
catch (Exception e) {
  throw new IOException("Error storing image in object: " + e);
}

}

然而,我无法让这个工作,我的意思是,它无法加载图像并转换它,我不知道问题是什么。

2 个答案:

答案 0 :(得分:0)

您确定图像路径是否正确,并且加载的图像没有损坏的图像。

我没有修改你的代码,我可以看到1778416 byes从图像文件中读取。

enter image description here

答案 1 :(得分:0)

我没有看到该程序有任何问题。可能是您的图像文件已损坏或图像路径不正确。

相关问题