Resizing a jpg picture sometimes fails.
787016Feb 18 2011 — edited Mar 3 2011I use this code to resize a jpg picture. Sometimes i get the resized image but sometimes instead of the resized image i get a black image.
It is a web application so this code is within a servlet.
Any hints?
+protected void doGet(......){+
+........+
+........+
response.setContentType(CONTENT_TYPE_IMAGE_JPG);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder
+(response.getOutputStream());+
encoder.encode(resizeImage(imageBytes));
+}+
+private BufferedImage resizeImage(byte [] imageBytes) {+
+try {+
InputStream inputStream = new ByteArrayInputStream(imageBytes);
BufferedImage bufferedImage = ImageIO.read(inputStream);
++return resize(bufferedImage, 250, 300);+
+}catch (Exception e) {+
throw new RuntimeException(e);
+}+
+}private BufferedImage resize(BufferedImage img, int newW, int newH) {+
int w = img.getWidth();
int h = img.getHeight();
BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());
Graphics2D g = dimg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);
g.dispose();
return dimg;
+}+