图片压缩功能以及转码,只列举了压缩,转码方法在内
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
public class ImageTest {
public static void main(String args[]) throws Exception {
ImageTest test = new ImageTest();
test.test();
}
public void test() throws AWTException, FileNotFoundException, IOException {
File file = new File("C:/Users/Administrator/Desktop/logo.jpg");
System.out.println("源文件大小: " + file.length());
Long a = (1024 * 200) / file.length();
BufferedImage image = readMemoryImage(readBytes(new FileInputStream("C:/Users/Administrator/Desktop/logo.jpg")));
System.out.println(image.getHeight());
byte[] picByte = newCompressImage(image, a.floatValue());
FileOutputStream f = new FileOutputStream("C:/Users/Administrator/Desktop/2-4.jpg");
f.write(picByte);
f.close();
}
public static final BufferedImage readMemoryImage(byte[] imgBytes) throws IOException {
if (null == imgBytes || 0 == imgBytes.length)
throw new NullPointerException("the argument 'imgBytes' must not be null or empty");
ImageInputStream imageInputstream = new MemoryCacheImageInputStream(new ByteArrayInputStream(imgBytes));
Iterator
it = ImageIO.getImageReaders(imageInputstream);
while (it.hasNext()) {
ImageReader imageReader = (ImageReader) it.next();
imageReader.setInput(imageInputstream, true, true);
String suffix = imageReader.getFormatName().trim().toLowerCase();
int width = imageReader.getWidth(0);
int height = imageReader.getHeight(0);
System.out.printf("format %s,%dx%d\n", suffix, width, height);
try {
return imageReader.read(0, imageReader.getDefaultReadParam());
} catch (Exception e) {
imageReader.dispose();
}
}
imageInputstream.close();
throw new IOException("unsupported image format");
}
public static final byte[] readBytes(InputStream in) throws IOException {
if (null == in)
throw new NullPointerException("the argument 'in' must not be null");
try {
int buffSize = Math.max(in.available(), 1024 * 8);
byte[] temp = new byte[buffSize];
ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
return out.toByteArray();
} finally {
in.close();
}
}
private byte[] bufferedImageTobytes(BufferedImage image, String format) {
System.out.println(format + "格式开始打包" + getCurrentTime());
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(image, format, out);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(format + "格式完成打包-----" + getCurrentTime()
+ "----lenth------" + out.toByteArray().length);
return out.toByteArray();
}
private byte[] bufferedImageTobytes(BufferedImage image, float quality) {
System.out.println("jpeg" + quality + "质量开始打包" + getCurrentTime());
if (image == null) {
return null;
}
Iterator
iter = ImageIO
.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter) iter.next();
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
iwp.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
ColorModel colorModel = ColorModel.getRGBdefault();
iwp.setDestinationType(new javax.imageio.ImageTypeSpecifier(colorModel,
colorModel.createCompatibleSampleModel(16, 16)));
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IIOImage iIamge = new IIOImage(image, null, null);
try {
writer.setOutput(ImageIO
.createImageOutputStream(byteArrayOutputStream));
writer.write(null, iIamge, iwp);
} catch (IOException e) {
System.out.println("write errro");
e.printStackTrace();
}
System.out.println("jpeg" + quality + "质量完成打包-----" + getCurrentTime()
+ "----lenth----" + byteArrayOutputStream.toByteArray().length);
return byteArrayOutputStream.toByteArray();
}
private String getCurrentTime() {
Calendar c = new GregorianCalendar();
int hour = c.get(Calendar.HOUR_OF_DAY);
int min = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
int millsecond = c.get(Calendar.MILLISECOND);
String time = hour + "点" + min + "分" + second + "秒" + millsecond;
return time;
}
private byte[] newCompressImage(BufferedImage image, float quality) {
if (image == null) {
return null;
}
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JPEGEncodeParam param = JPEGCodec.getDefaultJPEGEncodeParam(image);
param.setQuality(quality, false);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(
byteArrayOutputStream, param);
System.out.println("newCompressive" + quality + "质量开始打包"
+ getCurrentTime());
try {
encoder.encode(image);
} catch (Exception ef) {
ef.printStackTrace();
}
System.out.println("newCompressive" + quality + "质量打包完成"
+ getCurrentTime() + "----lenth----"
+ byteArrayOutputStream.toByteArray().length);
return byteArrayOutputStream.toByteArray();
}
}