Java:检测图像格式,调整大小(缩放)并另存为JPEG [英] Java: Detecting image format, resize (scale) and save as JPEG

查看:186
本文介绍了Java:检测图像格式,调整大小(缩放)并另存为JPEG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,它实际上有效,但并不完美,但确实如此,问题是调整大小的缩略图没有粘贴在白色的绘制矩形上,打破了图像的纵横比,这里是代码,可能有人建议我好好解决一下吗?

This is the code I have, it actually works, not perfectly but it does, the problem is that the resized thumbnails are not pasting on the white Drawn rectangle, breaking the images aspect ratio, here is the code, could someone suggest me a fix for it, please?

谢谢

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

}


推荐答案

您可以使用 图像 getScaledInstance 方法:

You can easily scale your image by using Image's getScaledInstance method:

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

获得缩放后的图像您可以将其转换回 BufferedImage 这里

Once you've obtained your scaled Image you can "convert" it back into a BufferedImage as described here.

最后,使用 ImageIO 将您的 BufferedImage 写入一个文件。

Finally, use the ImageIO class to write your BufferedImage to a file.

这篇关于Java:检测图像格式,调整大小(缩放)并另存为JPEG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆