Java applet - 以 png 格式保存图像

2024-04-30

我正在创建一个用于制作头像的简单小程序。您可以选择脸部、头发、眼睛等,然后将其作为 png 文件保存到光盘上。简单版本(为了简单起见,没有界面)如下所示:

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 保存一张图像,但我想“展平”两个或多个图像并保存它。任何帮助将不胜感激。我也同意,也许我的方法不正确,如果有任何更正,我将不胜感激。


当心快速编写且未经测试的代码!

基本概念是这样的: 您加载用于组合头像的图像,然后创建一个新的空图像并将头像的每个部分绘制到其上。之后,您只需将新创建的图像保存到文件中即可。

重要提示:对于未签名的小程序,getPath() 方法将因 AccessViolation 而失败。我想 FileChooser 在这里会是更好的方法。

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 "";
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Java applet - 以 png 格式保存图像 的相关文章

随机推荐