黑马程序员技术交流社区
标题:
【阳哥专栏】Java图片缩放
[打印本页]
作者:
王震阳老师
时间:
2014-9-20 18:42
标题:
【阳哥专栏】Java图片缩放
本帖最后由 就业指导-王震阳老师 于 2014-9-20 18:45 编辑
用Java实现图片的缩放。
package com.test;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
public class ImageUtil {
public static void resize(File originalFile, File resizedFile,
int newWidth, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage
public static void main(String[] args) throws IOException {
// File originalImage = new File("C:\\11.jpg");
// resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
// resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
File originalImage = new File("C:\\1207.gif");
resize(originalImage, new File("c:\\1207-0.jpg"),150, 0.7f);
resize(originalImage, new File("c:\\1207-1.jpg"),150, 1f);
}
}
复制代码
作者:
lishuliang28
时间:
2014-9-20 19:50
基本上可以看懂 但是自己不能弄个写处理啊
作者:
lanzy1989
时间:
2014-9-20 21:05
原理有没有解释一下的?
作者:
泡沫__opt
时间:
2014-9-21 10:42
学习额……
作者:
@for
时间:
2014-9-23 00:38
。。。。。。。。。有点难啊,
作者:
yuZhe_toString
时间:
2014-9-23 20:04
。。。。为什么没注释?
作者:
alpha.huai
时间:
2014-10-4 01:04
我找找原来做数学建模,有做图像相似拼接的代码。给个很多碎纸片图片,拼接。
作者:
剛☆_☆
时间:
2014-11-25 22:23
怎么没注释????
作者:
梦开始的地方
时间:
2015-1-17 17:14
貌似很实用,感谢分享
作者:
邓士林
时间:
2015-3-1 11:53
老师,com.sun.image.codec.jpeg.JPEGImageEncoder;这个包是不是要单独下载导入进去啊!java自带的没有呢
作者:
邓士林
时间:
2015-3-1 21:19
FileOutputStream out = new FileOutputStream(resizedFile);
这句为什么在Eclipse里面报错啊!java.io.FileNotFoundException
作者:
亚特兰蒂斯
时间:
2015-3-2 08:12
:lol:lol:lol:lol:lol:lol:lol:lol
作者:
shaoshuai
时间:
2015-3-2 11:09
很不错,学习了
作者:
yzmaodeng
时间:
2015-3-3 00:15
有用收走。。。。。哈哈
作者:
as604049322
时间:
2015-3-15 12:22
邓士林 发表于 2015-3-1 11:53
老师,com.sun.image.codec.jpeg.JPEGImageEncoder;这个包是不是要单独下载导入进去啊!java自带的没有呢 ...
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
要怎么玩啊???
作者:
as604049322
时间:
2015-3-15 12:57
运行成功了,压缩效果挺不错
package teacher.wangzengyang;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;
public class ImageUtil {
public static void resize(File originalFile,
int newWidth, float quality) throws IOException {
if (quality > 1) {
throw new IllegalArgumentException(
"Quality has to be between 0 and 1");
}
String[] name=originalFile.getName().split("\\.");
File resizedFile=new File(originalFile.getParentFile(),name[0]+"_new."+name[1]);
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight)
/ iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight,
newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null), BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = { 0, softenFactor, 0, softenFactor,
1 - (softenFactor * 4), softenFactor, 0, softenFactor, 0 };
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder
.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
} // Example usage
public static void main(String[] args) throws IOException {
// File originalImage = new File("C:\\11.jpg");
// resize(originalImage, new File("c:\\11-0.jpg"),150, 0.7f);
// resize(originalImage, new File("c:\\11-1.jpg"),150, 1f);
File originalImage = new File("E:\\desktop\\新建文件夹\\banner.jpg");
resize(originalImage,587, 0.3f);
// resize(originalImage,150, 1f);
}
}
复制代码
作者:
种马先生
时间:
2016-7-6 21:50
赶紧收藏
作者:
15114111253
时间:
2016-8-8 22:08
666666666666666666666666666666
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2