使用java将jpg图像添加在一起
我正在尝试拍摄多个具有相同尺寸(30*30)的 jpg 图像并创建一个图像。像这样:
Image i = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) i.getGraphics();
if (this instanceof Node) {
Image img;
img = getImageFromFile(Node.icon);
g2.drawImage(img, 0, 0, null);
}
if(this instanceof ForceNode){
Image img;
img = getImageFromFile(ForceNode.forceicon);
g2.drawImage(img, 0, 0, null);
}
if(this instanceof TunnelNode){
Image img;
img = getImageFromFile(TunnelNode.tunnelicon);
g2.drawImage(img, 0, 0, null);
}
....
public Image getImageFromFile(File file) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
Logger.getLogger(HackerGame.class.getName()).log(Level.SEVERE, null, e);
return null;
}
return image;
}
我意识到 G2D 存在一些问题并不是绝对必要的,但我的问题是这样的: 这些图像需要相互叠加才能创建完整的图像。大多数图像是整个图片的小区域,需要将它们放置在彼此之上(而不是相邻)才能创建实际图像。然而现在的问题是最后一个drawImage方法覆盖了整个图像,所以我留下了最后一个“图像位”而不是我编译的图像。
我怀疑这是因为我的照片的白色区域没有被视为透明,但我该如何解决这个问题。我几乎没有图像编码的经验,所以我有点尝试和错误:) 无论如何,帮助!
解决方案:
public void generateIcon() {
BufferedImage i = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
if (this instanceof Node) {
i = compileImages(i, Node.icon);
}
if(this instanceof ForceNode){
i = compileImages(i, ForceNode.forceicon);
}
if(this instanceof TunnelNode){
i = compileImages(i, TunnelNode.tunnelicon);
}
if (this instanceof EntranceNode) {
i = compileImages(i, EntranceNode.entranceicon);
}
if (this instanceof NetworkNode) {
i = compileImages(i, NetworkNode.networkicon);
}
if(this instanceof DataNode){
i = compileImages(i, DataNode.dataicon);
}
//if(this instanceof )
nodeicon = i;
}
public BufferedImage compileImages(BufferedImage image, File f) {
BufferedImage im = null;
try {
im = ImageIO.read(f);
for(int i = 0 ; i<image.getWidth();i++){
for(int j = 0 ; j<image.getHeight();j++){
int rgb = im.getRGB(i, j);
//System.out.println(i + " " + j + " " + rgb);
if(!(rgb < 1 && rgb > -2)){
image.setRGB(i, j, rgb);
//System.out.println("Printing " + i + " " + j + " " + rgb);
}
}
}
} catch (IOException e) {
Logger.getLogger(HackerGame.class.getName()).log(Level.SEVERE, null, e);
return null;
}
return image;
}
I am trying to take severial jpg images with the same dimensions(30*30) and create a single image. Like this:
Image i = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = (Graphics2D) i.getGraphics();
if (this instanceof Node) {
Image img;
img = getImageFromFile(Node.icon);
g2.drawImage(img, 0, 0, null);
}
if(this instanceof ForceNode){
Image img;
img = getImageFromFile(ForceNode.forceicon);
g2.drawImage(img, 0, 0, null);
}
if(this instanceof TunnelNode){
Image img;
img = getImageFromFile(TunnelNode.tunnelicon);
g2.drawImage(img, 0, 0, null);
}
....
public Image getImageFromFile(File file) {
Image image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
Logger.getLogger(HackerGame.class.getName()).log(Level.SEVERE, null, e);
return null;
}
return image;
}
I realize there are some issues with G2D not being strictly necessary, but my issue is this:
These images needs to be put on top of each other to create a whole image. Eash of the images are small areas of the whole picture, that needs to be put on top of(Not next to) each other to create the actual image. The problem right now however is that the last drawImage method overwrites the entire image, so i am left with the last "bit of image" instead of my compiled image.
I suspect this is because the white areas of my pictures are not being treated as transparent, but how do i get around this. I have next to no experience with image encoding so i am sort of going by trial and error:)
Anyway HELP!
Solution:
public void generateIcon() {
BufferedImage i = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
if (this instanceof Node) {
i = compileImages(i, Node.icon);
}
if(this instanceof ForceNode){
i = compileImages(i, ForceNode.forceicon);
}
if(this instanceof TunnelNode){
i = compileImages(i, TunnelNode.tunnelicon);
}
if (this instanceof EntranceNode) {
i = compileImages(i, EntranceNode.entranceicon);
}
if (this instanceof NetworkNode) {
i = compileImages(i, NetworkNode.networkicon);
}
if(this instanceof DataNode){
i = compileImages(i, DataNode.dataicon);
}
//if(this instanceof )
nodeicon = i;
}
public BufferedImage compileImages(BufferedImage image, File f) {
BufferedImage im = null;
try {
im = ImageIO.read(f);
for(int i = 0 ; i<image.getWidth();i++){
for(int j = 0 ; j<image.getHeight();j++){
int rgb = im.getRGB(i, j);
//System.out.println(i + " " + j + " " + rgb);
if(!(rgb < 1 && rgb > -2)){
image.setRGB(i, j, rgb);
//System.out.println("Printing " + i + " " + j + " " + rgb);
}
}
}
} catch (IOException e) {
Logger.getLogger(HackerGame.class.getName()).log(Level.SEVERE, null, e);
return null;
}
return image;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
迭代源图像的像素。如果它们不是白色(使用 getRGB(x,y) 来比较它们),请将它们写入目标图像。
Iterate the pixels of the source images. If they are not white (use
getRGB(x,y)
to compare them), write them to the destination image.