我使用drawImage()将大量脸部图像(face_50xx.png)粘贴到一个大画布(Faces.png)上,
但是每张脸都变成了整个黑色。
这是我的源代码:
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.Color;
public class maa{
static BufferedImage in;
static BufferedImage out;
public static void main(String[] args) {
String A = "face_";
String B = "png";
int j = 0;
try{
in = ImageIO.read(new File(A + 5001 + "." + B));
}
catch(java.io.IOException e){
}
out = new BufferedImage(1920, 14592, in.getType());
for(int i = 1; i < 760; i++){
String num;
j = i + 5000;
num = Integer.toString(j);
try{
in = ImageIO.read(new File("face_" + num + "." + "png"));
Graphics g = in.getGraphics();
g.drawImage(out, (i%10)*192, (i/10)*192, null);
}
catch(java.io.IOException e){
continue;
}
}
try{
ImageIO.write(out,"png",new File("Faces." + B));
}
catch(java.io.IOException e){
}
}
}
请教我什么问题。感谢。
答案 0 :(得分:2)
如,
catch(IOException e) {
e.printStackTrace();
}
程序的基本结构应该是:
create out image
get Out's Graphics object, g
for Loop through all of the `in` images
Draw each in image onto out using out's Graphics context, g
end for loop
dispose of g
Write the out image to file
修改:您在评论中说明,
Graphics g = in.getGraphics();
是将图像传输到g的命令,不是吗?
不,你有倒退的事情。可以将Graphics对象g视为一支笔,它允许您绘制从中获取的图像。因此,来自in
图像的图形对象g允许我在in
图像上绘制。
答案 1 :(得分:-2)
替换:
Graphics g = in.getGraphics();
g.drawImage(out, (i%10)*192, (i/10)*192, null);
by:in.getGraphics().drawImage(out, (i%10)*192, (i/10)*192, null);