如何在现有图像文件的顶部添加20像素的白色? [英] How to add 20 pixels of white at the top of an existing image file?
本文介绍了如何在现有图像文件的顶部添加20像素的白色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的图像尺寸为 w
,尺寸为 h
。在Java中,我需要创建尺寸为 w
乘以 h + 20
的图像,其中顶部 w
x 20像素
是白色,其余图像与原始图像相同。
I have an image of size w
by h
. In Java, I need to create an image that is size w
by h+20
where the top w
by 20 pixels
is white, and the rest of the image is the same as the original image.
基本上,我想知道如何在现有缓冲图像的顶部添加20像素的白色。
Essentially I want to know how I can add 20 pixels of white to the top of an existing buffered image.
所以这有点像这样:
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...
}
推荐答案
建议:
- 使用
GraphicsConfiguration.createCompatibleImage(int width,int height)
创建一个BufferedImage
的宽度相同,但高度为+20。 - 使用
BufferedImage.createGraphics()
获取此图像的Graphics2D
对象。 - 使用
Graphics.setColor(Color c)
和Graphics.fillRect(int x,int y ,int宽度,int高度)
绘制白色顶部 - 使用
Graphics.drawImage(Image img,int x,int y,ImageObserver观察器)
将原始图像绘制到新图像的指定坐标。 - 要保存新图像,请阅读编写/保存图像教程。
- Use
GraphicsConfiguration.createCompatibleImage(int width, int height)
to create aBufferedImage
of the same width, but with a height that's +20. - Use
BufferedImage.createGraphics()
to obtain theGraphics2D
object of this image. - Use
Graphics.setColor(Color c)
andGraphics.fillRect(int x, int y, int width, int height)
to draw the white top - Use
Graphics.drawImage(Image img, int x, int y, ImageObserver observer)
to draw the original image to the specified coordinates of the new image. - To save the new image, read the Writing/Saving an Image tutorial.
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);
}
}
这篇关于如何在现有图像文件的顶部添加20像素的白色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文