gpt4 book ai didi

java - 动态裁剪签名图像

转载 作者:行者123 更新时间:2023-11-30 06:19:13 24 4
gpt4 key购买 nike

我正在开发一个 Java 应用程序,它可以从 Wacom USB 平板电脑捕获客户签名,创建带有签名的 320x200 像素白色背景图像并将其存储到数据库中,以便稍后将其打印为 PDF。

在保存它们之前,我想在签名上裁剪这些图像。我的意思是,一个客户可以在平板电脑的左侧区域签名,另一个客户可以在右侧区域签名。因此,所有签名在签名图像中都会有不同的位置和无用的空白。例如,different signature images我想要类似 this 的东西

所以我的问题是,这可能吗?在保存之前是否有任何选项可以在签名上动态裁剪这些图像?知道所有签名都有不同的大小并不能总是从相同的位置进行裁剪。或者更好,例如,在平板电脑上打印一个矩形,迫使客户在其中签名? (不知道这是否可能,我想是的)。

谢谢。

最佳答案

我 promise 采用并行流方法。在这里。

未裁剪的图像 (png):http://assets-cdn.github.com/images/modules/open_graph/github-mark.png

public static void main(String[] args) throws Exception {
BufferedImage img = ImageIO.read(new URL("http://assets-cdn.github.com/images/modules/open_graph/github-mark.png"));

BufferedImage sigImage = cropSignature(img);

SwingUtilities.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setContentPane(new JLabel(new ImageIcon(sigImage)));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}

private static BufferedImage cropSignature(BufferedImage img) {
// Iterating over height has better CPU cache performance.
Rectangle sigRect = IntStream.range(0, img.getHeight())
.parallel()
.mapToObj(y -> getHorizontalSpan(img, y))
.collect(() -> new Rectangle(0, 0, -1, -1),
(r1, r2) -> r1.add(r2),
(r1, r2) -> r1.add(r2));

return img.getSubimage(sigRect.x, sigRect.y, sigRect.width, sigRect.height);
}

private static Rectangle getHorizontalSpan(BufferedImage img, int y) {
// Don't parallelize. The 'combiner' is therefore not necessary and may be null.
return IntStream.range(0, img.getWidth())
.filter(x -> isSignatureColor(img.getRGB(x, y)))
.collect(() -> new Rectangle(0, 0, -1, -1),
(r, x) -> r.add(x, y),
null);
}

private static boolean isSignatureColor(int rgb) {
// Easiest criteria, but fails on JPG files because it doesn't use a tolerance.
return rgb != -1;
}

关于java - 动态裁剪签名图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48542030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com