gpt4 book ai didi

java - 使用 Java 小程序裁剪从网络摄像头拍摄的图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:20:37 24 4
gpt4 key购买 nike

我的需求如下。

Take a photo using webcam and provide an edit button to crop and save the image using a re-sizable rectangle.

我已经完成了网络摄像头的编码,并使用小程序拍摄了照片并成功保存了图像。但是很难使用可调整大小的矩形启用编辑功能。我可以使用 Jcrop、jquery 进行裁剪,但问题是如何将图像从 applet 获取到 JSP。

或者有什么方法可以使用 Applet 本身使用矩形裁剪图像。

最佳答案

有点像...

enter image description here

public class ResizeCrop {

public static void main(String[] args) {
new ResizeCrop();
}

public ResizeCrop() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
} catch (InstantiationException ex) {
} catch (IllegalAccessException ex) {
} catch (UnsupportedLookAndFeelException ex) {
}

JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new CropPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class CropPane extends JPanel {

private BufferedImage background;
private Rectangle cropBounds;

public CropPane() {
try {
background = ImageIO.read(new File("/Users/swhitehead/Dropbox/MT008.gif"));
} catch (IOException exp) {
exp.printStackTrace();
}

MouseHandler handler = new MouseHandler();

addMouseListener(handler);
addMouseMotionListener(handler);
}

@Override
public Dimension getPreferredSize() {
return new Dimension(background.getWidth(), background.getHeight());
}

protected Rectangle getCropBounds() {
Rectangle actualBounds = null;
if (cropBounds != null) {
int x = cropBounds.x;
int y = cropBounds.y;
int width = cropBounds.width;
int height = cropBounds.height;

if (width < 0) {
x += width;
width -= (width * 2);
}
if (height < 0) {
y += height;
height -= (height * 2);
}

actualBounds = new Rectangle(x, y, width, height);
System.out.println(actualBounds);
}
return actualBounds;
}

@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2d = (Graphics2D) g.create();
if (background != null) {
int x = (getWidth() - background.getWidth()) / 2;
int y = (getHeight() - background.getHeight()) / 2;
g2d.drawImage(background, x, y, this);
}

Rectangle drawCrop = getCropBounds();
if (drawCrop != null) {
Color color = UIManager.getColor("List.selectionBackground");
g2d.setColor(color);
Composite composite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.fill(drawCrop);
g2d.setComposite(composite);
g2d.draw(drawCrop);
}
}

public class MouseHandler extends MouseAdapter {

@Override
public void mouseReleased(MouseEvent e) {
cropBounds = null;
repaint();
}

@Override
public void mousePressed(MouseEvent e) {
cropBounds = new Rectangle();
cropBounds.setLocation(e.getPoint());
repaint();
}

@Override
public void mouseDragged(MouseEvent e) {
if (cropBounds != null) {
Point p = e.getPoint();
int width = p.x - cropBounds.x;
int height = p.y - cropBounds.y;
cropBounds.setSize(width, height);
repaint();
}
}
}
}
}

或者您可以进行反向选择...

enter image description here

只需将选择绘制代码(在 paintComponent 方法中)替换为...

Rectangle drawCrop = getCropBounds();
if (drawCrop != null) {

Area area = new Area(new Rectangle(0, 0, getWidth() - 1, getHeight() - 1));
area.subtract(new Area(drawCrop));

Color color = UIManager.getColor("List.selectionBackground");
g2d.setColor(color);
Composite composite = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
g2d.fill(area);
g2d.setComposite(composite);
g2d.draw(area);
}

这里的重要部分是不要使用 cropBounds 字段,您必须调用 getCropBounds 因为它会纠正负矩形 ;)

代码示例清除了 mouseRelease 上的裁剪,但您可以保留矩形直到用户执行其他操作,例如双击...

关于java - 使用 Java 小程序裁剪从网络摄像头拍摄的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12917028/

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