JAVA 图片的缩放,和压缩,PNG背景透明
来源:互联网 发布:中金云金融大数据公司 编辑:程序博客网 时间:2024/04/29 23:18
public class ImgFactory {
private Logger log=LoggerFactory.getLogger(ImgFactory.class);
private File _saveFile;
private BufferedImage _sourceBufferedImage;
public static final int LETF=1;
public static final int RIGHT=2;
public static final int CENTER=3;
public static final int ABOVE=4;
public static final int BELOW=5;
private BufferedImage _waterMarker;
private int markerX;
private int markerY;
private int markerWidth;
private int markerHeight;
public ImgFactory(String soursePath,String savePath) throws IOException{
this(new File(soursePath),new File(savePath));
}
public ImgFactory(File _sourseFile,File _saveFile) throws IOException{
if(_sourseFile==null || _saveFile==null){
throw new NullPointerException("图片文件不能为null");
}
if(!_sourseFile.exists()){
throw new FileNotFoundException(_sourseFile.getAbsolutePath()+"not found!");
}
this._sourceBufferedImage=ImageIO.read(_sourseFile);
this._saveFile=_saveFile;
}
public void compress(int width,int height) throws IOException{
//---缩放图片
Image _compressImage=_sourceBufferedImage.getScaledInstance(width, height,Image.SCALE_SMOOTH);
//----图像透明,设置
BufferedImage _bufferedImage=new BufferedImage(width, height,this._sourceBufferedImage.getType());
Graphics graphics=_bufferedImage.getGraphics();
Graphics2D graphics2d=(Graphics2D) graphics;
//---绘制图片
graphics2d.drawImage(_compressImage, 0, 0, null);
saveImg(_bufferedImage);
}
public void compress(float radio) throws IOException{
int _width=_sourceBufferedImage.getWidth();
int _height=_sourceBufferedImage.getHeight();
float _toWidth=_width;
float _toHeight=_height;
if(radio!=0){
_toWidth*=radio;
_toHeight*=radio;
}
compress(Math.round(_toWidth), Math.round(_toHeight));
}
public void setWaterMarker(String imgPath,int x,int y,int width,int height) throwsIOException{
this._waterMarker=ImageIO.read(new File(imgPath));
this.markerX=x;
this.markerY=y;
this.markerWidth=width;
this.markerHeight=height;
}
public void zoom(int width,int height,boolean isCompress) throws IOException{
if(isCompress){
compress(width, height);
}else{
BufferedImage _bufferedImage=new BufferedImage(width, height,this._sourceBufferedImage.getType());
Graphics graphics=_bufferedImage.getGraphics();
Graphics2D graphics2d=(Graphics2D) graphics;
//---绘制图片
graphics2d.drawImage(this._sourceBufferedImage,0,0,width,height,null);
//--
saveImg(_bufferedImage);
}
}
public void zoom(float radio,boolean isCompress) throws IOException{
int _width=_sourceBufferedImage.getWidth();
int _height=_sourceBufferedImage.getHeight();
float _toWidth=_width;
float _toHeight=_height;
if(radio!=0){
_toWidth*=radio;
_toHeight*=radio;
}
zoom(Math.round(_toWidth), Math.round(_toHeight) ,isCompress);
}
public String getFileSuffix(File file) {
if (file == null) {
return null;
}
return getFileSuffix(file.getName());
}
public String getFileSuffix(String fileName) {
if (fileName == null){
return null;
}
String suffix = "";
int i = fileName.lastIndexOf('.');
if (i > 0 && i < fileName.length() - 1) {
suffix = fileName.substring(i + 1);
}
return suffix;
}
public void setSourceFile(File sourseFile) throws IOException{
this._sourceBufferedImage=ImageIO.read(sourseFile);
}
public void setSourceFile(String path) throws IOException{
setSaveFile(new File(path));
}
public void setSaveFile(File _saveFile) {
this._saveFile = _saveFile;
}
public void setSaveFile(String path){
setSaveFile(new File(path));
}
public void saveImg(BufferedImage bufferedImage) throws IOException{
Graphics2D graphics2d=(Graphics2D)bufferedImage.getGraphics();
//----水印图片不等于NULL,给图片添加水印
if(this._waterMarker!=null){
if(bufferedImage.getWidth() < this.markerWidth || bufferedImage.getHeight() <this.markerHeight){
log.error("图片小于水印图片大小!添加失败");
}else{
//---绘制水印
graphics2d.drawImage(this._waterMarker, markerX, markerY, markerWidth,markerHeight, null);
}
}
graphics2d.dispose();
ImageIO.write(bufferedImage, getFileSuffix(_saveFile), _saveFile);
}
//---测试
public static void main(String[] args) {
try {
ImgFactory ifs= new ImgFactory("D:/1.png", "D:/2.png");
ifs.setWaterMarker("D:/3.png", 0, 0, 50, 50);
ifs.zoom(0.5f,false);
System.out.println("成功!!!");
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
注:png透明设置,BufferedImage _bufferedImage=new BufferedImage(width, height, BufferedImage.TRANSLUCENT);
转自:http://blog.sina.com.cn/s/blog_bca9d7e80101cdcr.html
- JAVA 图片的缩放,和压缩,PNG背景透明
- java 创建PNG格式的透明背景的图片
- 关于Java生成背景透明的png图片
- 【转】关于Java生成背景透明的png图片
- IE6下png图片和png背景透明
- IE6下png图片和png背景透明
- IE6下png图片和png背景透明
- php 缩放gif和png图透明背景变成黑色的解决方法
- 解决java压缩图片透明背景变黑色的问题
- 笔记:jpg图片和png图片的区别(之一):png图片白色背景可透明,jpg图片不可以
- Java修改图片png格式透明背景大小分辨率
- 完美解决ie6不支持png 透明图片 和标签背景的问题
- png图片和背景变透明的方法兼容各个浏览器
- metinfo 5.0图片水印bug修改和支持透明背景png水印功能的增加
- Java裁剪压缩PNG图片,透明背景色变黑问题解决
- 老外写的支持png图片的透明,背景定位
- 在IE6中使用背景透明的PNG图片
- 浏览器不支持PNG图片背景透明的解决办法
- 数据库设计总结学习
- getopt函数的使用和实例
- node to traverse cannot be null
- 实验6线程及其同步—哲学家问题的线程
- plsa 公式推导
- JAVA 图片的缩放,和压缩,PNG背景透明
- 【收藏】Android开源项目和 框架
- [5月6日的脚本] 检测指定数据库的索引碎片百分比 (SQL)
- Effective C++ 读书笔记之Part2.Constructors, Destructors, and Assignment Operators
- Failed to install *.apk on device 'emulator-5554': timeout
- jenkins的构建触发中Poll SCM和Build periodically的区别
- 深入理解JVM内部结构
- 二叉查找树
- 字符串编码解析代码分析