?? jpgtest.java
字號(hào):
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.ImageIO;
public class JpgTest {
public static void jpg_logo(String jpgName, String logoText) throws Exception {
File _file = new File(jpgName+".jpg"); //讀入文件
Image src = ImageIO.read(_file); //構(gòu)造Image對(duì)象
int w0 = src.getWidth(null); //得到源圖寬
int h0 = src.getHeight(null); //得到源圖長(zhǎng)
int w2 = 800; //=w0/2
int h2 = 600; //=h0/2
int fontSize = 32;
//縮小一 半為(800,600)
BufferedImage tag = null;
tag = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0,w2, h2, null); //繪制縮小后的圖
//標(biāo)注水印
Graphics g = tag.getGraphics();
g.setColor(Color.RED); //以下設(shè)置前景色BLACK
// g.setXORMode(Color.RED);
g.setFont(new Font("MyFont", Font.BOLD, fontSize)); //PLAIN,BOLD,ITALIC
// g.drawString(logoText, 10, 10+fontSize);
g.drawString(logoText, w2-fontSize*(logoText.length()+3)/2, h2-10);
g.dispose();
//保存文件,輸出到文件流
FileOutputStream out = new FileOutputStream(jpgName+"_800.jpg");
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG編碼
//System.out.print(width+"*"+h0);
}finally{
out.close();
}
}
public static void jpg_logo() throws Exception {
File _file = new File("test_old.jpg"); //讀入文件
Image src = ImageIO.read(_file); //構(gòu)造Image對(duì)象
int w0=src.getWidth(null); //得到源圖寬
int h0=src.getHeight(null); //得到源圖長(zhǎng)
//縮小一半
BufferedImage tag = null;
tag = new BufferedImage(w0/2,h0/2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src,0,0,w0/2,h0/2,null); //繪制縮小后的圖
//標(biāo)注水印
Graphics g = tag.getGraphics();
//g.setColor(Color.BLACK); //以下設(shè)置前景色
g.setXORMode(Color.GREEN);
g.setFont(new Font("MyFont", Font.ITALIC, 24));
g.drawString("www.Test.com", w0/4, h0/4);
g.dispose();
//保存文件
FileOutputStream out = new FileOutputStream("test_new.jpg"); //輸出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG編碼
//System.out.print(width+"*"+h0);
out.close();
}
public static void jpg_mini(String fn, int factor) throws Exception {
String f0 = fn+".jpg";
File f = new File(f0); //讀入文件
Image src = ImageIO.read(f); //構(gòu)造Image對(duì)象
int w0 = src.getWidth(null); //得到源圖寬
int h0 = src.getHeight(null); //得到源圖長(zhǎng)
if (w0<800) throw new Exception("w0<800");
//-------------------------
String f2 = fn+"_.jpg";
int w2 = w0 / factor;
int h2 = h0 / factor;
BufferedImage tag = new BufferedImage(w2, h2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, w2, h2, null); //繪制縮小后的圖
System.out.println(f0+"("+w0+"*"+h0+") \t=> "+f2+"("+w2+"*"+h2+")");
//保存文件
FileOutputStream out = new FileOutputStream(f2); //輸出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG編碼
out.close();
}
public static void jpg_rename(String fn) throws Exception {
File f0 = new File(fn+".jpg");
f0.delete();
File f2 = new File(fn+"_.jpg");
f2.renameTo(f0);
}
public static String zeroInt(int n, int len) {
String s = ""+n;
for(int i=s.length(); i<len; i++) {
s = '0'+s;
}
return s;
}
public static void jpg_mini(int factor, String fx, int idx0, int idx2, int iLen) throws Exception {
//String fx = "D:/_拓展培訓(xùn)20060915/DSC_";
//int idx0 = 624;
//int idx2 = 656;
String fn = null;
for(int i=idx0; i<=idx2; i++) {
fn = fx + zeroInt(i, iLen); //長(zhǎng)度4 如:DSC_0168.JPG
try {
jpg_mini(fn, factor);
jpg_rename(fn);
}catch(Exception e){
System.out.println(fn+"..."+e);
}
}
}
public static void main(String args[]) throws Exception {
jpg_logo();
// jpg_mini("test_old");
// jpg_mini(4, "D:/", 1, 200, 4); //縮小4倍(0001-0200)
// jpg_logo("P1010105", "2005-4-9");
// jpg_logo("P1010086", "2005-4-9");
// jpg_logo("P1010017", "2005-4-18");
// Font f1 = Font.decode("宋體"); //System.out.println(s);
// Font f2 = Font.getFont("宋體"); //System.out.println(s);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -