Java 小程序 - 以 png 格式保存图像 [英] Java applet - saving an image in a png format

查看:26
本文介绍了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 保存一个图像,但我想展平"两个或更多图像并保存它.任何帮助将不胜感激.我也同意,也许我的方法不正确,如果有任何更正,我将不胜感激.

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.

推荐答案

注意快速编写和未经测试的代码!

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

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 导致的未签名小程序失败.我想 FileChooser 在这里会是更好的方法.

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 格式保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆