[英]Resize a byte array image in pixels
我有一個圖像存儲為字節數組。 無論大小有多大,我都希望始終將其調整為100x100像素。
我試圖將字節數組轉換為bufferedimage,調整大小並將其保存為bytearray。 但是使用以下代碼,圖像不再出現在我的頁面上。
byte[] pict; // this array contains the image
BufferedImage bufferedImage;
ByteArrayInputStream bais = new ByteArrayInputStream(pict);
try {
bufferedImage = ImageIO.read(bais);
bufferedImage = Scalr.resize(bufferedImage, 100);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, "jpg", baos );
baos.flush();
pict = baos.toByteArray();
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream o = response.getOutputStream();
o.write(pict);
這樣做是這樣的:
Image tmpImage = ImageIO.read(bais);
Image scaled = tmpImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
然后,要轉換為字節數組,將圖像轉換為bufferedimage,然后轉換為字節數組:
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpg", baos);
baos.flush();
pict = baos.toByteArray();
baos.close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.