簡體   English   中英

Java:檢測圖像格式,調整大小(縮放)並保存為JPEG

[英]Java: Detecting image format, resize (scale) and save as JPEG

這是我的代碼,它實際上工作,不完美,但確實如此,問題是調整大小的縮略圖不粘貼在白色的繪制矩形上,打破圖像寬高比,這里是代碼,有人可以建議我修復好嗎,拜托?

謝謝

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ImageScalerImageIoImpl implements ImageScaler {

 private static final String OUTPUT_FORMAT_ID = "jpeg";

 // Re-scaling image
 public byte[] scaleImage(byte[] originalImage, int targetWidth,
   int targetHeight) {

  try {
   InputStream imageStream = new BufferedInputStream(
     new ByteArrayInputStream(originalImage));
   Image image = (Image) ImageIO.read(imageStream);

   int thumbWidth = targetWidth;
   int thumbHeight = targetHeight;

   // Make sure the aspect ratio is maintained, so the image is not skewed
         double thumbRatio = (double)thumbWidth / (double)thumbHeight;
         int imageWidth = image.getWidth(null);
         int imageHeight = image.getHeight(null);
         double imageRatio = (double)imageWidth / (double)imageHeight;
         if (thumbRatio < imageRatio) {
           thumbHeight = (int)(thumbWidth / imageRatio);
         } else {
           thumbWidth = (int)(thumbHeight * imageRatio);
         }

   // Draw the scaled image
   BufferedImage thumbImage = new BufferedImage(thumbWidth,
     thumbHeight, BufferedImage.TYPE_INT_RGB);
   System.out.println("Thumb width Buffered: " + thumbWidth + " || Thumb height Buffered: " + thumbHeight);

   Graphics2D graphics2D = thumbImage.createGraphics();
   // Use of BILNEAR filtering to enable smooth scaling
   graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
     RenderingHints.VALUE_INTERPOLATION_BILINEAR);
   // graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

   // White Background
   graphics2D.setPaint(Color.WHITE);
   graphics2D.fill(new Rectangle2D.Double(0, 0, targetWidth,
     targetHeight));
   graphics2D.fillRect(0, 0, targetWidth, targetHeight);

   System.out.println("Target width: " + targetWidth + " || Target height: " + targetHeight);

   // insert the resized thumbnail between X and Y of the image 
   graphics2D.drawImage(image, 0, 0, thumbWidth, thumbHeight, null);

   System.out.println("Thumb width: " + thumbWidth + " || Thumb height: " + thumbHeight);

   // Write the scaled image to the outputstream
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   ImageIO.write(thumbImage, OUTPUT_FORMAT_ID, out);
   return out.toByteArray();

  } catch (IOException ioe) {
   throw new ImageResizingException(ioe);
  }
 }

}

您可以使用ImagegetScaledInstance方法輕松擴展圖像:

BufferedImage img = ImageIO.read(new File("image.jpg"));
int scaleX = (int) (img.getWidth() * 0.5);
int scaleY = (int) (img.getHeight() * 0.5);

Image newImg = img.getScaledInstance(scaleX, scaleY, Image.SCALE_SMOOTH);

一旦你得到你的縮放Image ,你可以“轉換”它放回一個BufferedImage描述在這里

最后,使用ImageIO類將BufferedImage寫入文件。

暫無
暫無

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

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