我正在尝试创建一个图像,通过将像素从旧位置复制到新坐标,为Java上的现有图像添加边框。到目前为止,这就是我所做的:
public static NewPic border (NewPic p, int borderWidth, Pixel borderColor) {
int w = p.getWidth();
int h = p.getHeight();
Pixel src[][] = p.getBitmap();
Pixel tgt[][] = new Pixel[h][w];
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
tgt[y][x + y + borderWidth] = src[x][y]; // this is probably where I a messing up
}
}
return new NewPic(tgt);
}
在我评论的行中不确定我做错了什么。我是Java新手。有人可以给我一些指导吗?
答案 0 :(得分:5)
一种方法是使用基于Swing的边框。
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
class ImageBorder {
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout());
// to contrast the 'picture frame' border created below
gui.setBorder(new LineBorder(Color.BLUE, 12));
Image image = // your image here..
new BufferedImage(400,50,BufferedImage.TYPE_INT_RGB);
JLabel l = new JLabel(new ImageIcon(image));
Border b1 = new BevelBorder(
BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.DARK_GRAY);
Border b2 = new LineBorder(Color.GRAY, 12);
Border b3 = new BevelBorder(
BevelBorder.LOWERED, Color.LIGHT_GRAY, Color.DARK_GRAY);
Border bTemp = new CompoundBorder(b1,b2);
Border b = new CompoundBorder(bTemp,b3);
l.setBorder(b);
gui.add(l);
JOptionPane.showMessageDialog(null, gui);
}
};
// Swing GUIs should be created and updated on the EDT
// http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
SwingUtilities.invokeLater(r);
}
}
答案 1 :(得分:3)
您可以创建一个BorderedBufferedImage
,其int
borderThickness
,borderColor
颜色和BufferedImage
。
This website也可能会提供一些帮助:
import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;
import javax.swing.BorderFactory;
class testImagePanel{
public static void main(String[] args){
BufferedImage image = null;
ImagePanel imagePanel = null;
try{
image = ImageIO.read(new File("Pictures/pl.jpg"));
imagePanel = new ImagePanel(image);
}catch(IOException e){
System.err.println("Trying to read in image "+e);
}
JFrame frame = new JFrame("Example");
frame.add(imagePanel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
public class ImagePanel extends JPanel {
BufferedImage image;
Dimension size;
public ImagePanel(BufferedImage image) {
this.image = image;
this.size = new Dimension();
size.setSize(image.getWidth(), image.getHeight());
this.setBackground(Color.WHITE);
this.setBorder(BorderFactory.createLineBorder(Color.RED, 1));
}
@Override
protected void paintComponent(Graphics g) {
// Center image in this component.
int x = (getWidth() - size.width)/2;
int y = (getHeight() - size.height)/2;
g.drawImage(image, x, y, this);
}
@Override
public Dimension getPreferredSize() { return size; }
}
答案 2 :(得分:1)
我使用以下逻辑为任何图像添加边框:
BufferedImage source = ImageIO.read(original);
int borderedImageWidth = width + (borderLeft * 2);
int borderedImageHeight = height + (borderTop * 2);
BufferedImage img = new BufferedImage(borderedImageWidth, borderedImageHeight, BufferedImage.TYPE_3BYTE_BGR);
img.createGraphics();
Graphics2D g = (Graphics2D) img.getGraphics();
g.setColor(Color.YELLOW);
g.fillRect(0, 0, borderedImageWidth, borderedImageHeight);
g.drawImage(source, borderLeft, borderTop, width + borderLeft, height + borderTop, 0, 0, width, height, Color.YELLOW, null);
File output = File.createTempFile("output", ".png");
ImageIO.write(img, "png", outputFile);
这将在黄色矩形上绘制图像,该矩形的大小比图像大,从而为图像提供边框。
答案 3 :(得分:1)
我知道这个帖子是从2013年开始的,但我遇到了这个问题,我有一个解决方案可以保留OP试图做的事情:
public static NewPic border (NewPic p, int borderWidth, Pixel borderColor)
{
int w = p.getWidth();
int h = p.getHeight();
Pixel src[][] = p.getBitmap();
Pixel tgt[][] = new Pixel[h + borderWidth * 2][w + borderWidth * 2];
for (int x = 0; x < w + borderWidth; x++)
{
for (int y = 0; y < h + borderWidth; y++)
{
if (x >= borderWidth && x < w - borderWidth && y >= borderWidth && y < h - borderWidth)
{
tgt[x][y] = src[x - borderWidth][y - borderWidth];
}
else
{
tgt[x][y] = borderColor;
}
}
}
return new NewPic(tgt);
}
答案 4 :(得分:0)
清洁Java最简单的方法:
String imagePath = "this/is/your/image.jpg";
BufferedImage myPicture = ImageIO.read(new File(imagePath));
Graphics2D g = (Graphics2D) myPicture.getGraphics();
g.setStroke(new BasicStroke(3));
g.setColor(Color.BLUE);
g.drawRect(10, 10, myPicture.getWidth() - 20, myPicture.getHeight() - 20);
ImageIO.write(myPicture, "jpg", new File(imagePath));