裁剪使用Java小程序从网络摄像头拍摄的图像 [英] Cropping an Image taken from Webcam using Java applet
问题描述
我的要求如下:
使用网络摄像头拍摄照片,并提供了一个编辑按钮裁切并使用重相当大的矩形保存图像。
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的形象。
I have done the coding for webcam and taken the photo using an applet and successfully saved the image. But it is difficult get the editing feature enabled using a re-sizable rectangle. I can crop using Jcrop, jquery but the problem is how to get the image taken from applet to the JSP.
,或有什么办法,我可以使用小程序本身使用的是矩形裁剪图像。
or is there any way I can use the Applet itself to crop the image using a rectangle.
推荐答案
喜欢的东西...
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();
}
}
}
}
}
或者你可以倒的选择...
Or you can have an inverted selection...
仅仅通过更换这个选择油漆code(在的paintComponent
法)...
Simply by replacing the selection paint code (in the paintComponent
method) with this...
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
因为它可以解决对于负矩形)
The important part here, is DON'T use the cropBounds
field, you must call getCropBounds
as it corrects for a negative rectangle ;)
在code例如将清除 mouseRelease
作物,但你可以保持矩形,直到用做别的东西,比如双击...
The code example clears the crop on mouseRelease
, but you could keep the rectangle until the use does something else, like double clicks...
这篇关于裁剪使用Java小程序从网络摄像头拍摄的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!