我正在尝试让我的Jframe完全匹配我的图像尺寸,以便当我尝试通过绘制矩形来获取某个区域的Rectangle2D坐标时,它为我提供了它在屏幕上出现的位置的真实坐标。实际图像。
此解决方案的目标是将PDF转换为图像,使用可视映射器识别特定区域,然后使用PDFBox(PDFTextStripperbyArea)针对该区域进行提取。
以下代码给出的坐标未提取所需区域。
这是代码:
public class PDFVisualMapper extends JFrame {
BufferedImage image = null;
public static void main(String[] args) throws IOException {
new PDFVisualMapper();
}
public PDFVisualMapper() throws IOException {
this.setSize(1700, 2200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
ArrayList<Shape> shapes = new ArrayList<Shape>();
Point startDrag, endDrag;
public PaintSurface() throws IOException {
image = ImageIO.read(new File("C:\\Users\\Rusty\\Desktop\\temp\\Test_PDF-1.png"));
if ( image != null ) {
Dimension size = new Dimension(image.getWidth(null), image.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
}
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startDrag = new Point(e.getX(), e.getY());
endDrag = startDrag;
repaint();
}
public void mouseReleased(MouseEvent e) {
Shape r = makeRectangle(startDrag.x, startDrag.y, e.getX(), e.getY());
shapes.add(r);
startDrag = null;
endDrag = null;
repaint();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endDrag = new Point(e.getX(), e.getY());
repaint();
}
});
}
private void paintBackground(Graphics2D g2) {
g2.setPaint(Color.LIGHT_GRAY);
for (int i = 0; i < getSize().width; i += 10) {
Shape line = new Line2D.Float(i, 0, i, getSize().height);
g2.draw(line);
}
for (int i = 0; i < getSize().height; i += 10) {
Shape line = new Line2D.Float(0, i, getSize().width, i);
g2.draw(line);
}
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
paintBackground(g2);
Color[] colors = { Color.YELLOW, Color.MAGENTA, Color.CYAN, Color.RED, Color.BLUE, Color.PINK };
int colorIndex = 0;
g2.drawImage(image, null, 0, 0);
g2.setStroke(new BasicStroke(2));
g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.50f));
for (Shape s : shapes) {
g2.setPaint(Color.BLACK);
g2.draw(s);
g2.setPaint(colors[(colorIndex++) % 6]);
g2.fill(s);
}
if (startDrag != null && endDrag != null) {
g2.setPaint(Color.LIGHT_GRAY);
Shape r = makeRectangle(startDrag.x, startDrag.y, endDrag.x, endDrag.y);
g2.draw(r);
System.out.println(r.getBounds2D());
}
}
}
private Rectangle2D.Float makeRectangle(int x1, int y1, int x2, int y2) {
return new Rectangle2D.Float(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x1 - x2), Math.abs(y1 - y2));
}
}
有人可以帮忙吗?
这可能更简单:JLabel在contentpane中使用FlowLayout:
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class WarpImage {
public static void main(String[] args) throws IOException {
displayImage();
}
private static void displayImage() throws IOException{
URL url = new URL("http://www.digitalphotoartistry.com/rose1.jpg");
BufferedImage image = ImageIO.read(url);
ImageIcon icon= new ImageIcon(image);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
JLabel lbl= new JLabel();
lbl.setIcon(icon);
frame.add(lbl);
frame.pack();
//check size :
Rectangle bounds = lbl.getBounds();
System.out.println(bounds.getWidth() +"-"+ bounds.getHeight());
frame.setVisible(true);
}
}
如果想要定义安装左侧的图片,请在 ; MUI 预定义常量 !define MUI_ABORTWARNING !define MUI_ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico" !define MUI_UNICON "${NSISDIR}\Contrib\Graphics\Icons\modern-uninstall.ico
我面临着将图片放入div的问题。基本上div是卡片的标题。图片的方向和大小不同。我应该怎么做才能让他们适应? 照片在div
问题内容: 首先,关于SO将图像复制到python中的剪贴板的问题导致答案用PIL和win32clipboard将图像写入python中的Windows剪贴板?,这仅对Python 2.x有用。-我尝试过,但是没有用。我克服了一个问题:Python 3.0中没有StringIO和cStringIO模块 ,但是碰到了另一个: 因此,对于Python 3再次提出相同的问题-如何在Python 3中将图
当我运行progressdialog出现在屏幕上时,它突然停止运行,并发出通知“应用程序停止了,再试一次”,然后我在android manitor上重复了这个错误,当然我首先要做的是:
我知道java DynamoDB的SDK被认为是异步非阻塞的,但我的问题是: Q、 dynamo更新是否仍需要与operator publishOn在另一个线程上,以便在反应性上下文中更安全? <代码>单声道。fromFuture(table.update())。publishOn()
我在Java 8应用程序中使用了处理,它在IntelliJ上运行良好。Gradle导出所有平台的fat jar(下面的代码)打包依赖项后出现问题: 每当我试图打开扩展PApplet的窗口时,它都会引发以下异常: 通用域名格式。约甘普。opengl。GLException:配置文件[GL4bc、GL4、GL3bc、GL3、GLS3、GL2、GLS2]在设备窗口GraphicsDevice[type.