簡體   English   中英

無法獲得有效的方法

[英]Can't get the method to work

我在這里有此方法,它將圖像轉換為字節數組。

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

}

但是,我無法使它正常工作,我的意思是,它無法加載圖像並進行轉換,而且我不知道問題可能出在哪里。

您確定圖像路徑正確並且加載的圖像沒有損壞的圖像。

我沒有修改您的代碼,因此可以看到從圖像文件讀取的1778416 byes。

在此處輸入圖片說明

我沒有看到程序有什么問題。 也許您的圖片文件已損壞或圖片路徑不正確。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM