以下是一个图片合成的代码,可是合成后,图片有点轻微失真,图片的大小有点变小(……k),望高手指教,如何能避免失真,使图片质量提高 p.v 8aN+^
]aGDM^
.RkJ4cD$q
import com.sun.image.codec.jpeg.JPEGImageEncoder; K)1'G!A O
import java.io.IOException; QP<=T5T
import com.sun.image.codec.jpeg.JPEGEncodeParam; ?6j{~yN
import javax.imageio.ImageIO; qzCP! 9
import java.awt.AlphaComposite; JE9lj+KdJ
import java.io.File; IqBbL<R}
import java.awt.Graphics2D; Ng F-1}.
import com.sun.image.codec.jpeg.JPEGCodec; }4t] 80F H
import java.io.FileOutputStream; X WF; kk
import java.awt.image.BufferedImage; l=u
import java.awt.Graphics; TMJu<x$r@
import javax.imageio.stream.FileImageInputStream; >mCX6
F/@L[WzqK
public class twoinone20070703 { S3 oP1GS
public twoinone20070703() { G! SILz,
} q}8D/ART
m\3Y(4Q}E
public static void ImgCompose(String ImgBotton, String ImgTop, %NdGC*3t^u
String ImgProduct, int width, int height) { YVg@sx=j
BufferedImage bi1 = null; _|"gdH9b
int x = width; <{Rd$;~C
int y = height; 3lIIPr
BufferedImage bi2 = null; 1O=<~`4?
try { c2Kkn)z<
// FileImageInputStream temp = new FileImageInputStream(ImgBotton); m&UaH n
//javax.imageio.ImageIO.ImageInputStream lwB*%p
bi1 = javax.imageio.ImageIO.read(new FileImageInputStream (new File(ImgBotton))); (K-j7!K^
// bi1 = javax.imageio.ImageIO.read(new ImageInputStream(ImgBotton)); !n Upjo4Ti
bi2 = javax.imageio.ImageIO.read(new FileImageInputStream (new File(ImgTop))); ?5`04Ve&
} 4(CWe,OMG
catch (IOException e) { xl1HQI5
e.printStackTrace(); "9b@X=
} ^ Io?O
xC*[{{:
/*try { 3Acu_*%2
// FileImageInputStream temp = new FileImageInputStream(ImgBotton); Yn4k~lh
//javax.imageio.ImageIO.ImageInputStream zD#yn -s
bi1 = javax.imageio.ImageIO.read(new File(ImgBotton)); io);5! 4h
// bi1 = javax.imageio.ImageIO.read(new ImageInputStream(ImgBotton)); ? {8IFjfM
bi2 = javax.imageio.ImageIO.read(new File(ImgTop)); ^b~~u
} catch (IOException e) { v4l0c2
e.printStackTrace(); VzL!jb g
}*/ `}C&S
\qr5<PLQ
Graphics g = bi1.getGraphics(); G1-u'+|
//g.getColor(); a. uJJwp
g.setColor(g.getColor()); P voGwe(5
g.drawImage(bi2, x, y, null); x~ EC=]L
FileOutputStream out = null; //输出到文件流 n}dG) 1
2A}P=
try { n_uzV
out = new FileOutputStream(ImgProduct); &}N;O*}
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); 1&NCI*\,
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi1); xm?`8{rcF)
param.setQuality(1, false); //不压缩图像fals [KP5i/8
encoder.setJPEGEncodeParam(param); /p*~#$e97
encoder.encode(bi1); kn}+D\0UU
out.flush(); <" v5(JD5
out.close(); `oB%)D +
tKjdyHZ
} N'7H~C5P
catch (IOException e) { T%!hr Lg#
e.printStackTrace(); //合并图 96c&(y
} q,|^XNNSr
! o 5iJ)
/* Uz;lXdgG
p1背景图,p2印章,p为输出图。p2要是gif或png等支持透明图象的。 &5goQq[
K4:p;inV
x,y 为在背景图放置印章的坐标( 左上角开始算)。 &jxs_F
>v,,<Mn<
]~-,"o
在Java2D中设置画图的透明度: 3%k6Z( M
*/ ~[PL^:N0
uYmO lfT
float alpha = 0.0001f; // 透明度 K7[n:;B
try { |dX!s>f
BufferedImage bufferedImage = ImageIO.read(new File(ImgProduct)); ?By3i+M
Graphics2D g2d = bufferedImage.createGraphics(); R*x!58
// 设置透明 Ql@ogP4b
g2d.setComposite(AlphaComposite.getInstance( s5#5 0{5Z
AlphaComposite.SRC_ATOP, alpha)); // 开始 y"% sAnt&
//g2d.drawImage(); // 画图 N#/Iq^3: 8
g2d.drawImage(bi2, x, y, null); O+*GY
g2d.setComposite(AlphaComposite.getInstance( (,paD\
AlphaComposite.SRC_OVER)); // 结束 *PL2*"
5tCFCxx
} C.B/y K
catch (Exception e) { g-o O {
System.out.println("error of ImgCompose"); .L=G#r
} l0(v34G
} !hVx3=3
public static void main(String[] args) throws IOException { 4 LD*3
twoinone20070703 temp = new twoinone20070703(); 'Q-k.Uq~
temp.ImgCompose("e:\\img\\2.jpg","e:\\img\\57.jpg","e:\\img\\3.jpg",0,0); Az`;(W_?G`
} &;VBHIhJ
}
查看本文来源