ImageOperate.java
上传用户:u_thks
上传日期:2022-07-31
资源大小:1910k
文件大小:9k
- /*
- * 向指定的图片上写字,生成图片缩略图等。
- * Made In GamVan
- */
- package com.gamvan.image;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileOutputStream;
- import com.gamvan.tools.Arith;
- import com.sun.image.codec.jpeg.JPEGCodec;
- import com.sun.image.codec.jpeg.JPEGImageEncoder;
- // 缩略图
- public class ImageOperate {
- Image img1;
- String message = "";
- String filePath = ""; //文件所在的根目录, 绝对路径。
- String waterImg1 = "", waterImg2=""; //水印图片文件名
- String waterPath = ""; //水印图片所在路径,绝对路径
- String str = new String("www.GamVan.com");
-
-
-
- /**
- * 给图片加水印
- * @param oldFile 加水印前的文件名.
- * @param newFile 加水印后的文件名。
- * @return
- */
- public void waterImage(String oldFile, String newFile){
- filePath = filePath.replace("\","\\");
- filePath += "\\";
- try{
- File myFile = new File(filePath + oldFile); //指定文件名含路径
- Image src = javax.imageio.ImageIO.read(myFile); //构造Image对象
- int w=src.getWidth(null); //得到源图宽
- int h=src.getHeight(null); //得到源图长
- Graphics gim = src.getGraphics();
- BufferedImage tag = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(src,0,0,w,h,null);
- if(!waterImg1.equals("") && waterImg2.equals("")){
- Image img1 = javax.imageio.ImageIO.read(new File(waterPath+waterImg1));//构造Image对象
- int w1 = img1.getWidth(null); //得到源图宽
- int h1 = img1.getHeight(null); //得到源图长
- tag.getGraphics().drawImage(img1,w-w1-12,h-36,w1,h1,null);
- }
- if(!waterImg1.equals("") && !waterImg2.equals("")){
- Image img1 = javax.imageio.ImageIO.read(new File(waterPath+waterImg1));//构造Image对象
- int w1 = img1.getWidth(null); //得到源图宽
- int h1 = img1.getHeight(null); //得到源图长
- Image img2 = javax.imageio.ImageIO.read(new File(waterPath+waterImg2));//构造Image对象
- int w2 = img2.getWidth(null); //得到源图宽
- int h2 = img2.getHeight(null); //得到源图长
- tag.getGraphics().drawImage(img2,w-w2-12,h-36,w2,h2,null);
- tag.getGraphics().drawImage(img1,w-w1-w2-16,h-36,w1,h1,null);
- }
- FileOutputStream out=new FileOutputStream(filePath + newFile); //输出到文件流
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag); //JPEG编码
- gim.dispose();
- out.close();
- }catch(Exception e){
- message = waterPath + e.toString() + filePath;
- }
- }
-
- /**
- * 往图片上写字
- * @param oldFile
- * @param newFile
- * @return
- */
- public void writeImage(String oldFile, String newFile, String fontFamily, int fontSize){
- filePath = filePath.replace("\","\\");
- filePath += "\\";
- //设置字体
- if(fontFamily.trim().equals("")){
- fontFamily = "Tahoma";
- }
- if(fontSize<=0){
- fontSize = 12;
- }
- try{
- File myFile = new File(filePath + oldFile); //指定文件名含路径
- Image src = javax.imageio.ImageIO.read(myFile); //构造Image对象
- int w=src.getWidth(null); //得到源图宽
- int h=src.getHeight(null); //得到源图长
- //在你创立的image上写字
- Graphics gim = src.getGraphics();
- Font font = new Font(fontFamily, Font.BOLD, fontSize);
- gim.setFont(font);
- gim.setColor(Color.black);
-
- gim.drawString(str, w-(str.length()*10-1), h-12);
-
- gim.setColor(Color.lightGray);
- gim.drawString(str, w-(str.length()*10-0), h-13);
- BufferedImage tag = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
-
- tag.getGraphics().drawImage(src,0,0,w,h,null);
- FileOutputStream out=new FileOutputStream(filePath + newFile); //输出到文件流
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag); //JPEG编码
- gim.dispose();
- out.close();
- }catch(Exception e){
- message = e.toString();
- }
- }
-
- /**
- * 图片缩略图
- * @param w
- * @param h
- * @param oldFile
- * @param newFile
- * @throws Exception
- * @return
- */
- public String shrinkImage(int w, int h, String oldFile, String newFile) {
- String str = ""; //返回文件名
- filePath = filePath.replace("\","\\");
- filePath += "\\";
- double wd = 0;
- double hd = 0;
- String ws = "", hs = "";
- try{
- File myFile = new File(filePath + oldFile); //指定文件名含路径
- Image src = javax.imageio.ImageIO.read(myFile); //构造Image对象
- int width=src.getWidth(null); //得到源图宽
- int height=src.getHeight(null); //得到源图长
- if((w>0 && h==0) && w < width){
- // 计算出宽度的缩放比例。
- wd = Arith.div(Double.parseDouble(String.valueOf(w)),Double.parseDouble(String.valueOf(width)),10);
-
- hd = Arith.mul(wd,Double.parseDouble(String.valueOf(height)));
- hd = Arith.round(hd,0); //四舍五入保留小数点0位
- hs = String.valueOf(hd);
- hs = hs.substring(0, hs.indexOf("."));
-
- h = Integer.parseInt(hs) ; //按比例缩小高度
- if(h<1){
- h = height;
- }
- }
- else if((h > 0 && w == 0) && h < height){
- // 计算出高度的缩放比例。
- hd = Arith.div(Double.parseDouble(String.valueOf(h)),Double.parseDouble(String.valueOf(height)),10);
- wd = Arith.mul(hd,Double.parseDouble(String.valueOf(width)));
- wd = Arith.round(wd,0); //四舍五入保留小数点0位
- ws = String.valueOf(wd);
- ws = ws.substring(0, ws.indexOf("."));
- w = Integer.parseInt(ws) ; //按比例缩小高度
- if(w<1){
- w = width;
- }
- }
- else if((w>0 && h>0)){
-
- }
- else{
- w = width;
- h = height;
- }
- //同时指定宽高
- if(w > width){
- w =width-1;
- }
- if(h > height){
- h = height-1;
- }
- if(w<1){
- w = width;
- }
- if(h<1){
- h = height;
- }
- java.awt.image.BufferedImage tag = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
- tag.getGraphics().drawImage(src,0,0,w,h,null); //绘制缩小后的图
- FileOutputStream out=new FileOutputStream(filePath + newFile); //输出到文件流
- JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
- encoder.encode(tag); //JPEG编码
- out.close();
- str = newFile;
- }catch(Exception e){
- str = "";
- message = e.toString();
- }
- return str;
- }
-
-
-
-
- public String getMessage(){
- return this.message;
- }
- public void setFilePath(String filePath){
- if(filePath!=null){
- this.filePath = filePath;
- }else{
- this.filePath = "";
- }
- }
- public void setStr(String str){
- if(str!=null){
- this.str = str;
- }else{
- this.str = "club.GamVan.com";
- }
- }
- public void setWaterImg1(String str){
- if(str!=null){
- this.waterImg1 = str;
- }else{
- this.waterImg1 = "";
- }
- }
- public void setWaterImg2(String str){
- if(str!=null){
- this.waterImg2 = str;
- }else{
- this.waterImg2 = "";
- }
- }
- public void setWaterPath(String str){
- if(str!=null){
- this.waterPath = str;
- }else{
- this.waterPath = "";
- }
- }
-
-
- }