星期二, 7月 19, 2011

[Java] Resize Image as JPG

簡單的影像Resize程式

public class ResizeImage {

 static public byte[] resizeImageAsJPG(byte[] pImageData, int pMaxWidth)
   throws IOException {

  // Create an ImageIcon from the input image byte[]
  ImageIcon imageIcon = new ImageIcon(pImageData);
  int width = imageIcon.getIconWidth();
  int height = imageIcon.getIconHeight();

  // If the image is larger than the max width, we need to resize it
  if (pMaxWidth > 0 && width > pMaxWidth) {
   // Determine the shrink ratio
   double ratio = (double) pMaxWidth / imageIcon.getIconWidth();
   height = (int) (imageIcon.getIconHeight() * ratio);
   width = pMaxWidth;
  }

  // Create a new empty image buffer to "draw" the resized image into
  BufferedImage bufferedResizedImage = new BufferedImage(width, height,BufferedImage.SCALE_SMOOTH);
  // Create a Graphics object to do the "drawing"
  Graphics2D g2d = bufferedResizedImage.createGraphics();
  g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  // Draw the resized image
  g2d.drawImage(imageIcon.getImage(), 0, 0, width, height, null);
  g2d.dispose();
  // Now our buffered image is ready
  // Encode it as a JPEG
  ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
  JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(encoderOutputStream);
  encoder.encode(bufferedResizedImage);
  byte[] resizedImageByteArray = encoderOutputStream.toByteArray();
  return resizedImageByteArray;
 }
}

1 則留言:

留個話吧:)

其他你感興趣的文章

Related Posts with Thumbnails