try {
BufferedImage top = ImageIO.read(new File("img/顶色.png"))
BufferedImage bot = ImageIO.read(new File("img/底色.png"))
BufferedImage result = new BufferedImage(top.getWidth(),top.getHeight(),BufferedImage.TYPE_INT_ARGB)
for(int height = 0height<bot.getHeight()height++){
for(int width = 0width<bot.getWidth()width++){
result.setRGB(width, height, top.getRGB(width, height)|bot.getRGB(width, height))
}
}
ImageIO.write(result, "png", new File("img/result.png"))//write()函数将result对象中的内容保存到result.png图片中
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
目录结构
示例文件
ImageIO.write(BufferedImage, "JPG", File)================================
传入Component保存图像的方法,你试试看还有没有变色。
public void cutScreen(Component com) {
Rectangle rect = com.getBounds()
BufferedImage bi = (BufferedImage) com.createImage(rect.width,
rect.height)
Graphics g = bi.getGraphics()
com.paint(g)
g.dispose()
JFileChooser jfc = new JFileChooser()
jfc.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.isDirectory()
|| f.getName().toLowerCase().endsWith(".jpg")
}
public String getDescription() {
return "*.jpg"
}
})
int type = jfc.showSaveDialog(null)
if (type == 0) {
File file = jfc.getSelectedFile()
name = file.getName().toLowerCase()
if (!name.endsWith("jpg")) {
String path = file.getAbsolutePath()
file = new File(path + ".jpg")
for (int i = 0file.exists()i++) {
file = new File(path + "(" + i + ").jpg")
}
}
try {
if (!file.exists()) {
file.createNewFile()
}
ImageIO.write(bi, "JPG", file)
} catch (IOException e1) {
e1.printStackTrace()
}
}
}