package com.goldgrid.utils;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
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;
public class ThumbnailMaker {
/**
*jz 2018-9-26
* 生成缩略图
*/
/**
* @Description: 取得图片对象
* @param 要转化的图像的文件夹,就是存放图像的文件夹路径
* @date 2017年5月7日10:48:27
*/
public BufferedImage zoomImage(BufferedImage im, int toWidth , int toHeight) {
BufferedImage result = new BufferedImage(toWidth, toHeight, BufferedImage.TYPE_INT_RGB);
result.getGraphics().drawImage(im.getScaledInstance(toWidth, toHeight, java.awt.Image.SCALE_SMOOTH), 0, 0, null);
return result;
}
/**
*
* @Description: 取得图片对象
* @param 要转化的图像的文件夹,就是存放图像的文件夹路径
* @date 2017年5月7日10:48:27
*/
public BufferedImage getImageList(String ImgList, String[] type) throws IOException{
Map<String,Boolean> map = new HashMap<String, Boolean>();
for(String s : type) {
map.put(s,true);
}
BufferedImage imageList = null;
File file = null;
file = new File(ImgList);
try{
if(file.length() != 0 && map.get(getExtension(file.getName())) != null ){
imageList = javax.imageio.ImageIO.read(file);
}
}catch(Exception e){
imageList = null;
}
return imageList;
}
/**
* 把图片写到磁盘上
* @param im
* @param path eg: C://home// 图片写入的文件夹地址
* @param fileName DCM1987.jpg 写入图片的名字
* @date 2017年5月7日10:48:27
*/
public boolean writeToDisk(BufferedImage im, String path, String fileName) {
File f = new File(path + fileName);
String fileType = getExtension(fileName);
if (fileType == null)
return false;
try {
ImageIO.write(im, fileType, f);
im.flush();
return true;
} catch (IOException e) {
return false;
}
}
/**
*
* @Description: 生成图片
* @param String path , BufferedImage im, String fileFullPath
* @date 2017年5月7日10:48:27
*/
public boolean writeHighQuality(String path , BufferedImage im, String fileFullPath) throws IOException {
//return true;
FileOutputStream newimage = null;
try {
// 输出到文件流
newimage = new FileOutputStream(fileFullPath+"/"+path);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(im);
// 压缩质量
jep.setQuality(1f, true);
encoder.encode(im, jep);
//近JPEG编码
newimage.close();
return true;
} catch (Exception e) {
return false;
}
}
/**
*
* @Description: 取文件名的后缀
* @param String fileName 格式如:cn1100000213EA_1_xnl.jpg
* @date 2017年5月7日10:48:27
*/
public String getExtension(String fileName) {
try {
return fileName.split("\\.")[fileName.split("\\.").length - 1];
} catch (Exception e) {
return null;
}
}
public static void main(String[] args) throws IOException {
String a="d:\\i.jpg";
File file=new File(a);
FileInputStream fileInputStream=new FileInputStream(file);
byte[] b=new byte[819200];
fileInputStream.read(b);
InputStream in = new ByteArrayInputStream(b);
BufferedImage image = ImageIO.read(in);
System.out.println(image);
String outputFolder = "G:\\Eclipse\\eclipse\\EclipseOxygenDev\\.metadata\\.plugins\\org.eclipse.wst.server.core\\tmp6\\wtpwebapps\\iSolutions-BDE\\uploadpicture\\";
String fileName = "D:\\i.jpg";
ThumbnailMaker r = new ThumbnailMaker();
int toWidth=200;
int toHeight=240;
BufferedImage imageList = r.getImageList(fileName,new String[] {"jpg","png","gif"});
r.writeHighQuality("newFile.jpg",r.zoomImage(imageList,toWidth,toHeight),outputFolder);
}
}
in = new SmbFileInputStream(f);
/*byte[] b = new byte[8192];
int n = 0;
while ((n = in.read(b)) > 0){
out.write(b, 0, n);
}*/
/*
* 查看图片服务器上的图片时,将获取的图片流生成缩略图展示。
* jz 2018-9-27
*/
BufferedImage image = ImageIO.read(in);
ThumbnailMaker r = new ThumbnailMaker();
int toWidth=200;
int toHeight=240;
BufferedImage bufferedImage=r.zoomImage(image,toWidth,toHeight);
ImageIO.write(bufferedImage,"jpg",output);
is = new ByteArrayInputStream(output.toByteArray());
byte[] c = new byte[8192];
int m=0;
while ((m = is.read(c)) > 0){
out.write(c,0,m);
}