下面是图片合并的源代码,合并的过程是先算出合并前几张图片的最大宽和几张图片的高合计,然后生成一张最大宽和最大高的图片,问题是现在较小的图片合并后,空余部分的底色是黑色的,想要是白色的。哪位高手帮我看一下怎么能让空余部分的黑色变成白色。小弟感激不尽。
/**
* 合并图片
*
* @author
* @version 1.0.0 2010-6-17
*
*/
import java.io.File;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.imageio.ImageIO;
import java.util.Iterator;
import java.util.Map;public class MergeImage {
public String PluralToOne(Map _map,String outptah) {
int width = 0;
int height = 0;
int[] ImageArrayTmp =null;
int widthmax=0;
int heightmax=0;
int heighstt=0;
String iRtn="";
try {
Iterator<String> it=_map.keySet().iterator();
//设定生成新图片的最大宽和高
while (it.hasNext()) {
// 循环读取图片
File fileOne = new File(_map.get(it.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
if (width>widthmax){
widthmax=width;
}
heightmax=heightmax+height;
ImageRead.flush();
}
//生成新的最大图片地址空间
BufferedImage ImageNew = new BufferedImage(widthmax, heightmax, BufferedImage.TYPE_INT_RGB);
Iterator<String> itr=_map.keySet().iterator();
while (itr.hasNext()) {
//循环读取图片 写进新的图片地址空间
File fileOne = new File(_map.get(itr.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度 // 从图片中读取RGB
ImageArrayTmp = null;
ImageArrayTmp = ImageRead.getRGB(0, 0, width , height, null,0, width);
ImageNew.setRGB(0, heighstt, width, height, ImageArrayTmp, 0, width);// 设置部分的RGB
heighstt=heighstt+height;
ImageRead.flush();
}
File outFile = new File(outptah);
ImageIO.write(ImageNew, "jpg",outFile);// 写图片
ImageNew.flush();
} catch (OutOfMemoryError e) {
iRtn ="-1";
} catch (Exception e) {
e.printStackTrace();
}finally{
return iRtn;
}
} public static void main(String[] args) throws Exception {
Map _oMap =new HashMap();
_oMap.put(1,"c:\\1.jpg");
_oMap.put(2,"c:\\2.jpg");
MergeImage app=new MergeImage();
app.PluralToOne(_oMap, "c:\\test.jpg");
}
}
/**
* 合并图片
*
* @author
* @version 1.0.0 2010-6-17
*
*/
import java.io.File;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.imageio.ImageIO;
import java.util.Iterator;
import java.util.Map;public class MergeImage {
public String PluralToOne(Map _map,String outptah) {
int width = 0;
int height = 0;
int[] ImageArrayTmp =null;
int widthmax=0;
int heightmax=0;
int heighstt=0;
String iRtn="";
try {
Iterator<String> it=_map.keySet().iterator();
//设定生成新图片的最大宽和高
while (it.hasNext()) {
// 循环读取图片
File fileOne = new File(_map.get(it.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
if (width>widthmax){
widthmax=width;
}
heightmax=heightmax+height;
ImageRead.flush();
}
//生成新的最大图片地址空间
BufferedImage ImageNew = new BufferedImage(widthmax, heightmax, BufferedImage.TYPE_INT_RGB);
Iterator<String> itr=_map.keySet().iterator();
while (itr.hasNext()) {
//循环读取图片 写进新的图片地址空间
File fileOne = new File(_map.get(itr.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度 // 从图片中读取RGB
ImageArrayTmp = null;
ImageArrayTmp = ImageRead.getRGB(0, 0, width , height, null,0, width);
ImageNew.setRGB(0, heighstt, width, height, ImageArrayTmp, 0, width);// 设置部分的RGB
heighstt=heighstt+height;
ImageRead.flush();
}
File outFile = new File(outptah);
ImageIO.write(ImageNew, "jpg",outFile);// 写图片
ImageNew.flush();
} catch (OutOfMemoryError e) {
iRtn ="-1";
} catch (Exception e) {
e.printStackTrace();
}finally{
return iRtn;
}
} public static void main(String[] args) throws Exception {
Map _oMap =new HashMap();
_oMap.put(1,"c:\\1.jpg");
_oMap.put(2,"c:\\2.jpg");
MergeImage app=new MergeImage();
app.PluralToOne(_oMap, "c:\\test.jpg");
}
}
/**
* 合并图片
*
* @author
* @version 1.0.0 2010-6-17
*
*/
import java.io.File;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.imageio.ImageIO;
import java.util.Iterator;
import java.util.Map;public class MergeImage {
public String PluralToOne(Map _map,String outptah) {
int width = 0;
int height = 0;
int[] ImageArrayTmp =null;
int widthmax=0;
int widthtt=0;
int heightmax=0;
int heighstt=0;
String iRtn="";
try {
Iterator<String> it=_map.keySet().iterator();
//设定生成新图片的最大宽和高
while (it.hasNext()) {
// 循环读取图片
File fileOne = new File(_map.get(it.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
if (width>widthmax){
widthmax=width;
}
heightmax=heightmax+height;
ImageRead.flush();
}
//生成新的最大图片地址空间
BufferedImage ImageNew = new BufferedImage(widthmax, heightmax, BufferedImage.TYPE_INT_RGB);
Iterator<String> itr=_map.keySet().iterator();
while (itr.hasNext()) {
//循环读取图片 写进新的图片地址空间
File fileOne = new File(_map.get(itr.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
// 从图片中读取RGB
ImageArrayTmp = null;
ImageArrayTmp = ImageRead.getRGB(0, 0, width , height, null,0, width);
ImageNew.setRGB(0, heighstt, width, height, ImageArrayTmp, 0, width);// 设置部分的RGB
int white= Integer.parseInt("FFFFFF",16);
for(int i=width;i<widthmax;i++)
{
for(int j=heighstt;j<height+heighstt;j++)
{
ImageNew.setRGB(i,j,white);
}
}
heighstt=heighstt+height;
ImageRead.flush();
}
File outFile = new File(outptah);
ImageIO.write(ImageNew, "jpg",outFile);// 写图片
ImageNew.flush();
} catch (OutOfMemoryError e) {
iRtn ="-1";
} catch (Exception e) {
e.printStackTrace();
}finally{
return iRtn;
}
} public static void main(String[] args) throws Exception {
Map _oMap =new HashMap();
_oMap.put(1,"c:\\a.jpg");
_oMap.put(2,"c:\\s.jpg");
MergeImage app=new MergeImage();
app.PluralToOne(_oMap, "c:\\test.jpg");
}}
* 合并图片
*
* @author
* @version 1.0.0 2010-6-17
*
*/
import java.io.File;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import javax.imageio.ImageIO;
import java.util.Iterator;
import java.util.Map;public class MergeImage {
public String PluralToOne(Map _map,String outptah) {
int width = 0;
int height = 0;
int[] ImageArrayTmp =null;
int widthmax=0;
int widthtt=0;
int heightmax=0;
int heighstt=0;
String iRtn="";
try {
Iterator<String> it=_map.keySet().iterator();
//设定生成新图片的最大宽和高
while (it.hasNext()) {
// 循环读取图片
File fileOne = new File(_map.get(it.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
if (width>widthmax){
widthmax=width;
}
heightmax=heightmax+height;
ImageRead.flush();
}
//生成新的最大图片地址空间
BufferedImage ImageNew = new BufferedImage(widthmax, heightmax, BufferedImage.TYPE_INT_RGB);
Iterator<String> itr=_map.keySet().iterator();
Graphics g = ImageNew.getGraphics();
g.setColor(new Color(255,0,0));
while (itr.hasNext()) {
//循环读取图片 写进新的图片地址空间
File fileOne = new File(_map.get(itr.next()).toString());
BufferedImage ImageRead = ImageIO.read(fileOne);
width = ImageRead.getWidth();// 图片宽度
height = ImageRead.getHeight();// 图片高度
// 从图片中读取RGB
ImageArrayTmp = null;
ImageArrayTmp = ImageRead.getRGB(0, 0, width , height, null,0, width);
ImageNew.setRGB(0, heighstt, width, height, ImageArrayTmp, 0, width);// 设置部分的RGB
//画图片剩余的部分
g.fillRect(width, heighstt, widthmax-width, height);
heighstt=heighstt+height;
ImageRead.flush();
}
File outFile = new File(outptah);
ImageIO.write(ImageNew, "jpg",outFile);// 写图片
ImageNew.flush();
} catch (OutOfMemoryError e) {
iRtn ="-1";
} catch (Exception e) {
e.printStackTrace();
}finally{
return iRtn;
}
} public static void main(String[] args) throws Exception {
Map _oMap =new HashMap();
_oMap.put(1,"c:\\a.jpg");
_oMap.put(2,"c:\\s.jpg");
MergeImage app=new MergeImage();
app.PluralToOne(_oMap, "c:\\test.jpg");
}}
g.setColor(new Color(255,0,0));是红色。
g.setColor(new Color(255,255,255));这个是白色。同时也要感谢Inhibitory的回复。