How to add 20 pixels of white at the top of an existing image file?

I have a size image won h. In Java, I need to create an image the size w h+20where the top one wis 20 pixelswhite and the rest of the image is the same as the original image.

Essentially, I want to know how to add 20 pixels of white to the top of an existing buffered image.

So it would be something like:

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...
}
+5
source share
3 answers

Sentence:


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);
    }
}

enter image description here

+12

, . :

  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);
               }
          }
      }
    }

P.S. , . , .

+2

, BufferedImage Graphic2D. Graphic2D BufferedImage, .

, , .

+1

All Articles