1、package poi; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import
2、 java.util.Random; import javax.imageio.ImageIO; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGEncodeParam; import com.sun.image.codec.jpeg.JPEGImageEncoder; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class Base64 { /**
3、 * @param args * @throws IOException */ public static void main(String[] args){ String imgFile = "E:\\1.png";//待处理的图片 InputStream in = null; byte[] data = null; //读取图片字节数组 try { in = new FileInputStream(imgFile);
4、 data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); String imgStr = encoder.encode(data); BASE64De
5、coder decoder = new BASE64Decoder();
//Base64解码
try {
byte[] b = decoder.decodeBuffer(imgStr);
for(int i=0;i 6、random = new Random();
String sjs = "";
for(int i=0; i<10; i++){
String rand = String.valueOf(random.nextInt(10));
sjs += rand;
}
String imgFilePath = "E:/" + sjs + ".jpg";//新生成的图片
OutputStream out = new F 7、ileOutputStream(imgFilePath);
out.write(b);
out.flush();
out.close();
InputStream file = new FileInputStream(imgFilePath);
if(file.available() > 200000){
Base64.doCompress(imgFilePath, 300, 300, 1, "S_", true);
// 8、Base64.doCompress(imgFilePath, 300, 300, 1, "_S", true);
//固定值,没有前缀后缀
//Base64.doCompress(imgFilePath, 300, 300, 1, "", false);
}
file.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static Strin 9、g doCompress(String oldFile, int width, int height,
float quality, String smallIcon, boolean percentage) {
if (oldFile != null && width > 0 && height > 0) {
String newImage = null;
try {
File file = new File(oldFile);
// 文件不存在
if (!file.exists()) {
return null; 10、
}
/*读取图片信息*/
Image srcFile = ImageIO.read(file);
int new_w = width;
int new_h = height;
if (percentage) {
// 为等比缩放计算输出的图片宽度及高度
//接收固定值等比
//double rate1 = ((double) srcFile.getWidth(null)) / (double) width + 0.1;
//double rate2 = ((double) srcFi 11、le.getHeight(null)) / (double) height + 0.1;
//double rate = rate1 > rate2 ? rate1 : rate2;
//new_w = (int) (((double) srcFile.getWidth(null)) / rate);
//new_h = (int) (((double) srcFile.getHeight(null)) / rate);
//原图/1.1
new_w = (int) (((double) srcFile.getWidth(null 12、)) / 1.1);
new_h = (int) (((double) srcFile.getHeight(null)) / 1.1);
}
/*宽高设定*/
BufferedImage tag = new BufferedImage(new_w, new_h, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(srcFile, 0, 0, new_w, new_h, null);
/*压缩后的文件名 */
//加前缀
String pst = 13、 oldFile.substring(0,oldFile.lastIndexOf('/')+1);
String filePrex = oldFile.substring(oldFile.indexOf('/')+1, oldFile.lastIndexOf('.'));
newImage = pst + smallIcon + filePrex + oldFile.substring(oldFile.lastIndexOf('.'));
//加后缀
//String filePrex = oldFile.substring(0, oldFile.la 14、stIndexOf('.'));
//newImage = filePrex + smallIcon + oldFile.substring(filePrex.length());
/*压缩之后临时存放位置*/
FileOutputStream out = new FileOutputStream(newImage);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPE 15、GEncodeParam(tag);
/*压缩质量 */
jep.setQuality(quality, true);
encoder.encode(tag, jep);
out.close();
srcFile.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
} else {
return null;
}
}
}






