如何在现有图像文件顶部添加20像素的白色?

6
我有一张尺寸为w x h的图片。在Java中,我需要创建一张大小为w x h+20的图片,其中顶部w x 20像素是白色的,其余部分与原始图片相同。
基本上,我想知道如何在现有缓冲图像的顶部添加20个像素的白色。
代码应该是这样的:
public static void main (String[] args) {
  BufferedImage originalImage = [the original image with a specific file path];
    ...code to create a new image 20 pixels higher...
    ...code to paint originalImage 20 pixels down on the new image
    ...code to save the new image...
}

@Amir,“我想知道如何在现有的缓冲图像顶部添加20像素的白色。” - jjnguy
3个回答

13

建议:

  1. 使用GraphicsConfiguration.createCompatibleImage(int width, int height) 创建一个宽度相同,但高度增加 20 的 BufferedImage
  2. 使用BufferedImage.createGraphics()获得此图像的Graphics2D对象。
  3. 使用Graphics.setColor(Color c)Graphics.fillRect(int x, int y, int width, int height)绘制白色区域的顶部。
  4. 使用Graphics.drawImage(Image img, int x, int y, ImageObserver observer)在新图像的指定坐标上绘制原始图像。
  5. 要保存新图像,请参阅编写/保存图像教程。
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class ImageManipulationDemo {
    private static BufferedImage ORIGINAL;
    private static BufferedImage ALTERED;
    private static final GraphicsConfiguration config = 
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

    public static void main(String[] args) {
        try {
            loadImages();

            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();             
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void loadImages() throws IOException {
        ORIGINAL = ImageIO.read(
                ImageManipulationDemo.class.getResource("../resources/whitefro1.jpg"));
        ALTERED = config.createCompatibleImage(
                ORIGINAL.getWidth(), 
                ORIGINAL.getHeight() + 20);
        Graphics2D g2 = ALTERED.createGraphics();
        g2.setColor(Color.WHITE);
        g2.fillRect(0, 0, ALTERED.getWidth(), 20);
        g2.drawImage(ORIGINAL, 0, 20, null);
        g2.dispose();

        // Save image
        ImageIO.write(ALTERED, "PNG", new File("alteredImage.png"));
    }

    private static void createAndShowGUI() {
        final JFrame frame = new JFrame("Image Manipulation Demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setBackground(Color.BLUE.darker());
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new JLabel(new ImageIcon(ORIGINAL)));
        frame.getContentPane().add(new JLabel(new ImageIcon(ALTERED)));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

在这里输入图片描述


2
是的,这很有道理。例如:
  public static void main (String[] args) {
      BufferedImage originalImage = [the original image with a specific file path];
      BufferedImage newImage = new BufferedImage(originalImage.getWidth() + 20 , originalImage.getHeight() + 20 , YOUR_IMAGE_TYPE);
      for (int i = 0 ; i < newImage.getHeight() ; i++){
          for (int j = 0 ; j < newImage.getWidth() ; j++){
               if (j > 20 && i > 20){
                     newImage.setRGB(i,j, originalImage.getRGB(i - 20, j - 20));
               } else{
                     newImage.setRGB(i,j, YOUR_RGB_VAL);
               }
          }
      }
    }

附言:我希望这段代码是正确的。我没有测试过,只是即兴写下了它。


1

这是可能的,您需要调查 BufferedImageGraphic2D。您可以从 BufferedImage 中检索 Graphic2D 来操纵和绘制您的图像。

基本上,您应该创建一个具有更大尺寸的第二个图像,并在正确的位置将第一个图像绘制在其中,以在第一个图像中留下空白空间。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接