本文介绍了Java小程序 - 在png格式保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建头像制定一个简单的小程序。您可以选择的脸,头发,眼睛等,然后将其保存到光盘为PNG文件。简单的版本(不带接口的简单目的)是这样的:

I am creating a simple applet for avatar-making. You can choose face, hair, eyes, etc and then save it to a disc as a png file. The simple version (without the interface for the simplicity purpose) looks like this:

import java.awt.*;
import java.applet.*;
import java.net.*;

public class Example extends Applet
 {

 Image my_gif;
 Image my_gif2;
 URL base;
 MediaTracker mt;

 public void init()
 {
        mt = new MediaTracker(this);
     try {
             base = getDocumentBase();
     }
     catch (Exception e) {}

       my_gif = getImage(base,"1.gif");
       my_gif2 = getImage(base,"2.gif");

       mt.addImage(my_gif,1);
       mt.addImage(my_gif2,2);

      try {
           mt.waitForAll();
      }
      catch (InterruptedException  e) {}
  }
 public void paint(Graphics g)
 {
       g.drawImage(my_gif,0,0,this);
     g.drawImage(my_gif2,0,0,this);
 }
 }

此示例由两个文件。在运行的时候他们以正确的方式可见。现在我想将它保存到光盘 - 我可以节省使用BufferedImage的一个图像,但我想扁平化两个或两个以上的图像和保存。任何帮助将大大AP preciated。我也同意,也许我的做法是不正确的,将是任何更正感谢。

This example consists of two files. When run they are visible in a correct way. Now I would like to save it to a disc - I can save one image using BufferedImage but I want to "flatten" two or more images and save it. Any help would be greatly appreciated. I also agree that perhaps my approach is not the right one and would be grateful for any corrections.

推荐答案

谨防快速写入和未经考验code!

的基本概念是:
加载从中结合化身图像,然后创建一个新的空图像,绘制头像的每一部分到它。之后,你刚才新建的图像保存到一个文件中。

The basic concept is this:You load the images from which you combine the avatar, then you create a new empty image and draw each part of the avatar onto it. After that you just save the newly created image to a file.

重要提示:本的getPath()方法将失败了AccessViolation的未签名的Applet原因。我想文件选择器将是一个更好的办法在这里。

Important note: The getPath() Method will fail for unsigned applets cause of a AccessViolation. I suppose a FileChooser would be a better approach here.

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.imageio.ImageIO;

public class Avatar {
    // Graphics
    private GraphicsConfiguration config = GraphicsEnvironment
    		.getLocalGraphicsEnvironment().getDefaultScreenDevice()
    		.getDefaultConfiguration();

    private BufferedImage faceImage;
    private BufferedImage hairImage;
    private BufferedImage mouthImage;

    public Avatar(final String face, final String hair, final String mouth,
    		final String out) {

    	// Load the Image parts
    	faceImage = load(face);
    	hairImage = load(hair);
    	mouthImage = load(mouth);

    	// Combine the images
    	BufferedImage outImage = combine();

    	// Save the new image
    	try {
    		ImageIO.write(outImage, "png", new File(getPath()
    				+ "screenshot.png"));
    	} catch (IOException e) {
    	}
    }

    // Combine
    private BufferedImage combine() {
    	// Create an empty image
    	BufferedImage buffer = create(200, 400, true);

    	// Get the graphics context
    	Graphics2D g = buffer.createGraphics();

    	// Draw all 3 images onto the empty one
    	g.drawImage(faceImage, 0, 0, null);
    	g.drawImage(hairImage, 0, 0, null);
    	g.drawImage(mouthImage, 0, 0, null);

    	// Get rid of the graphics context
    	g.dispose();
    	return buffer;
    }

    // Image
    private URL getURL(final String filename) {
    	URL url = Avatar.class.getResource(filename);
    	return url;
    }

    private BufferedImage load(final String file) {
    	URL filename = getURL(file);
    	if (filename == null) {
    		return null;
    	} else {
    		try {
    			return ImageIO.read(filename);
    		} catch (IOException e) {
    			return null;
    		}
    	}
    }

    private BufferedImage create(final int width, final int height,
    		final boolean alpha) {
    	BufferedImage buffer = config.createCompatibleImage(width, height,
    			alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
    	return buffer;
    }

    // Path
    private final String getPath() {
    	String path = currentPath();
    	if (currentPath().toLowerCase().endsWith(".jar")) {
    		path = path.substring(0, path.lastIndexOf("/") + 1);
    	}
    	return path;

    }

    private String currentPath() {
    	try {
    		return this.getClass().getProtectionDomain().getCodeSource()
    				.getLocation().toURI().getPath();
    	} catch (URISyntaxException e) {
    		return "";
    	}
    }
}

这篇关于Java小程序 - 在png格式保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:42