public class ImageUtils {
private Image srcImage = null;
private File srcFile = null;
private File destFile = null;
private String fileSuffix = null;
private int imageWidth = 0;
private int imageHeight = 0;
public ImageUtils(String fileName) throws IOException {
this(new File(fileName));
}
public ImageUtils(File fileName) throws IOException {
File _file = fileName;
_file.setReadOnly();
this.srcFile = _file;
this.fileSuffix = _file.getName().substring(
(_file.getName().indexOf(".") + 1),
(_file.getName().length()));
this.destFile = new File(this.srcFile.getPath().substring(0,
(this.srcFile.getPath().lastIndexOf(".")))
+ PIC_TEMPORARY + "." + this.fileSuffix);
srcImage = javax.imageio.ImageIO.read(_file);
//得到图片的原始大小, 以便按比例压缩。
imageWidth = srcImage.getWidth(null);
imageHeight = srcImage.getHeight(null);
System.out.println("width: " + imageWidth);
System.out.println("height: " + imageHeight);
}
/**
* 强制压缩/放大图片到固定的大小
* @param w int 新宽度
* @param h int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
//得到合适的压缩大小,按比例。
if ( imageWidth >= imageHeight)
{
w = w;
h = (int)Math.round((imageHeight * w * 1.0 / imageWidth));
}
else
{
h = h;
w = (int)Math.round((imageWidth * h * 1.0 / imageHeight));
}
//构建图片对象
BufferedImage _image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
//绘制缩小后的图
_image.getGraphics().drawImage(srcImage, 0, 0, w, h, null);
//输出到文件流
FileOutputStream out = new FileOutputStream(destFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image);
out.flush();
out.close();
}
}
下边是另外一种模式,不过原理都是一样的,就是等比例压缩,比较原图的宽高来确定压缩的比例。
Java中图片的缩放(按比例)
现在的一些应用中,我们常常需要把原始图片存到数据库中去,而显示的时候可能是各种缩小后的图片(长宽比例不变,不然图片会变形)。比如图片管理的时候先显示一系列小图片,点击后显示放大的图片。但是现在的图片有的height>width,而有的height<width,长宽比例个不相同,当显示一系列小图片的时候可能不整齐,一般网页上的方法是用固定的正方形做底色,然后在上面显示缩小后的图片。下面的resizeImage方法正是此问题的solution,输入参数maxDim就是固定的正方形的长度:
此外,此方法的类需要:
import com.sun.jimi.core.raster.JimiRasterImage;
import com.sun.jimi.core.Jimi;
/** resize the image in byte stream(format: [in]GIF, JPG; [out]JPG)
* @param in - the binary stream of the original picture in GIF or JPG
* @param maxDim - the bigger one between height and width after the picture is resized
* @return the binary stream of the resized picture in JPG
*/
public static byte[] resizeImage(byte[] in,int maxDim)
{
try
{
Image inImage=Toolkit.getDefaultToolkit().createImage(in);
ImageIcon inImageIcon = new ImageIcon(in);
int imh = inImageIcon.getIconHeight();
int imw = inImageIcon.getIconWidth();
double scale;
if( imh <= maxDim && imw <= maxDim )
scale = 1;
else if( imh > imw )
scale = (double) maxDim / (double) imh;
else
scale = (double) maxDim / (double) imw;
int scaledW = (int) (scale * imw);
int scaledH = (int) (scale * imh);
Image img = inImage.getScaledInstance(scaledW, scaledH, Image.SCALE_FAST);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
JimiRasterImage raster = Jimi.createRasterImage(img.getSource());
// --java.io.ByteArrayOutputStream
Jimi.putImage("image/jpeg", raster, outStream);
outStream.flush();
outStream.close();
return outStream.toByteArray();
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
分享到:
相关推荐
java 处理图片大小等比例缩放;图片裁剪;支持图片压缩不失真;支持图片添加水印;支持动态图的处理;支持自定义图片大小的转换 等 压缩包中内附使用说明
应为图片太大,所以按照一定比例缩放
高清缩放图像、切割图像、图像类型转换、彩色转黑白、文字水印、图片水印等 功能非常强大,并且很清晰,请给好评:)
java图片缩放 java缩略图 缩略图 java图片处理
java处理的图片,缩放,比例处理,压缩等java处理的图片,缩放,比例处理,压缩等
按比例缩放图片
用JAVA获取视频文件中的帧图片并等比缩放,java 获取视频中图片 缩放比例 javacv
采用双线性算法实现图片的缩放,今天,经过多次实现终于完成了图片的缩放,来给大家分享一下
这个小项目主要是利用java Thumbnaillator类库实现对图片的缩放功能,代码参考自慕课网Java实现图片等比例缩略图教学视频,目的为需求源码的小伙伴提供一个参考,如有问题大家再一起交流(附:资源积分是必选项,...
java gif图片缩放代码及帮助文档,很好用的。一看就懂
NULL 博文链接:https://seaie.iteye.com/blog/1678015
文件内含java 处理图片大小等比例缩放,指定颜色改变颜色。根据不同图片设置阴影图片。
网上流传的Java处理图片的代码,虽然可对图片进行简单处理,但效果并不理想。 为了解决这个问题,我也是在网上找了好久,看了很多资料,功夫不负有心人,最终找到了一个处理图片十分棒的开源框架。 特此拿出来与大家...
完整的例子,对YUV图片进行缩放,代码里有几个缩放方法
上传图片的同时可以生成两个小的缩略图,一个为100X100,一个为300X300。
java实现的图片处理,主要功能图片缩放,按比例缩放,截剪
Android下载图片和缩放图片的实现 适用于减少内存的使用
// 图片比例 double pw = sw; double ph = sh; double m = (double) sw / pw; double n = (double) sh / ph; System.out.println(m); int wth = (int) (destWidth * m); int hth = (int) (destHeight * n); ...
主要介绍了Java实现的微信图片处理工具类,可实现针对图片的裁剪、合并、等比例缩放、旋转、识别等各种常见的图片处理功能,需要的朋友可以参考下
如果图片宽度或高度大于表单域,将自动按比例缩放到合适大小。 addHtml(String templKey,String key,String htmlStr),向模板中一个表单域添加一段html代码, Html代码将被解析成实际显示内容添加到表单域中。注:...