📄 jpeghandle.java
字号:
/*
* 创建日期 2004-10-18
*
* TODO
*/
package com.ntsky.file;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* @author Administrator
* 图像处理
* TODO
*/
public class JPEGHandle {
private JPEG jpeg ;
private File file ;
private String contextPath;
public JPEGHandle(File file,JPEG jpeg,String contextPath){
this.file = file;
this.jpeg = jpeg;
this.contextPath = contextPath;
}
protected void encoderPic(){
Image src; //构造Image对象
try {
src = ImageIO.read(file);
int width=src.getWidth(null); //得到源图宽
int height=src.getHeight(null); //得到源图长
int tempWidth = jpeg.getPicWidth();
int tempHeight = jpeg.getPicHeight();
// 当图片高度和宽度确定的情况下
/*if((tempWeidth!=0)&&(tempHeight==0)){
tempHeight =
}*/
// 生成图片
builderPic(drawPic(src,tempWidth,tempHeight));
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
/**
* 绘制图片框架
* @param src
* @param weigth
* @param height
* @return
*/
private BufferedImage drawPic(Image src,int weigth,int height){
BufferedImage bufferedImage = new BufferedImage(weigth,height,BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(src,0,0,weigth,height,null); //绘制缩小后的图
return bufferedImage;
}
/**
* 生成图片的各种方案
* 宽度
* @param tag
*/
/*public String getWidth(int width,int height,int tempHeight){
}*/
/**
* 高度
* @param tag
*/
//public String getWidth(){
//}
/**
* 生成图片部分
* @param tag
*/
private void builderPic(BufferedImage BufferedImage){
FileOutputStream out; //输出到文件流
try {
String picPath = jpeg.getPicPath(); // 图片路径
String picName = jpeg.getPicName(); // 图片名
// 文件的路径
String filePath = file.getPath();
filePath = FileUtil.htmlEncode(filePath);
System.out.println("filePath = " + filePath);
//文件名
String fileName = file.getName();
int tempLength = fileName.indexOf("."); //文件名到“.”分割
String picSuffix = fileName.substring(tempLength);
String outPicPath = "";
// 图片路径已知的情况
if(picPath == null){
String tempPath = filePath.substring(0,filePath.lastIndexOf("/"))+"/";
// 文件名未知的情况
if(picName == null){
outPicPath = tempPath+fileName.substring(0,tempLength)+"_small"+picSuffix;
//FileUtil.makeFile(outPicPath);
}
else{
// 有文件名的情况
outPicPath = tempPath+picName+picSuffix;
// FileUtil.makeFile(outPicPath);
}
}
else{
// 设置small图片路径的配置
// 目录的创建
FileUtil.makeDir(picPath,FileUtil.htmlEncode(contextPath));
// System.out.println("picPath = " + picPath + FileUtil.htmlEncode(contextPath));
if(picName == null){
// 不设置文件名的情况
outPicPath = FileUtil.getAllPath(FileUtil.htmlEncode(contextPath) + picPath) + fileName.substring(0,tempLength)+"_small"+picSuffix;
}
else{
// 有文件名的情况
outPicPath = FileUtil.getAllPath(FileUtil.htmlEncode(contextPath) + picPath) + picName + picSuffix;
}
//System.out.println("outPicPath = " + outPicPath);
}
out = new FileOutputStream(outPicPath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(BufferedImage); //近JPEG编码
out.close();
// 是否想删除Source原来的图片
if(jpeg.isDelSourcePic())
file.delete();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -