2012-10-16 145 views
3

我的要求如下。使用Java小程序从网络摄像头裁剪拍摄的图像

使用网络摄像头拍摄照片,并提供一个编辑按钮,使用可调整大小的矩形裁剪和保存图像。

我已经完成了摄像头的编码,并使用applet拍摄了照片并成功保存了图像。但使用一个可缩放的矩形很难获得编辑功能。我可以裁剪使用jcrop,jquery,但问题是如何从applet获取图像到JSP。

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

+0

图像的最终目的是什么?这是用户下载,用于装饰网页..? –

+0

谢谢安德鲁。最终目的是在用户裁剪后保存图像。我想通过从小程序发送并从JSP进行访问来输入会话对象。 –

+0

感谢您解释,我认为@MadProgrammer覆盖了它。 –

回答

3

喜欢的东西...

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作物,但你可以保持矩形,直到用做别的东西,比如双击...

+0

谢谢Madprogrammer。我认为这会对我有用,我会测试并让你知道。再次感谢您 –

+0

感谢MadProgrammer。这完全符合我的要求。 –

+0

(从其他评论注释)很高兴你得到它排序。 :)当你有机会时,请[接受](http://meta.stackexchange.com/a/65088/155831)答案。 –

1

您可以通过这样的方式从现有的BufferedImage裁剪矩形图像:

BufferedImage newImage = image.getSubimage(x, y, width, height); 
+0

谢谢丹..问题是与重新设置矩形 –

0

你的东东d到仅仅3张支持声明的作物图像

/* Arpana */ 

import java.awt.Graphics; 
import java.awt.Image; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.image.CropImageFilter; 
import java.awt.image.FilteredImageSource; 
import java.awt.image.ImageFilter; 
import java.awt.image.ImageProducer; 
import java.awt.*; 
import java.applet.*; 

/* 
<applet code="CropImg" width=1024 height=768> 
</applet> 
*/ 

public class CropImg extends Applet implements MouseListener 
{ 
     private static final long serialVersionUID = 1L; 
     Image img; 
     Image crop; 
     boolean cropped; 
     public void init() 
     { 
      //setSize(880, 660); 
      img = getImage(getCodeBase(), "Appu.jpg"); 
      addMouseListener(this); 

     } 

    public void mouseClicked(MouseEvent e) 
    { 
     int x = getX(); 
     int y = getY(); 

     if (cropped) 
     { 
      cropper(x, y); 
     } 
     else 
     { 
      cropped = false; 
     } 
     repaint(); 

    } 
    public void cropper(int x, int y) 
    { 

     if(x < 500) 
     { 
      x = 200; 
     } 
     if(x > 500) 
     { 
      x = 500; 
     } 
     if(y < 500) 
     { 
      y = 200; 
     } 
     if(y > 500) 
     { 
      y = 500; 
     } 
    //420,330 
     int cropX, cropY; 
     cropX = x; //-1024; 
     cropY = y; //- 768; 
     ImageFilter imgF = new CropImageFilter(500, 500, 120, 439); 
     ImageProducer imgP = new FilteredImageSource(img.getSource(), imgF); 
     crop = createImage(imgP); 
    } 
    public void mouseEntered(MouseEvent e) 
    { 

    } 

    public void mouseExited(MouseEvent e) 
      { 
      } 

    public void mousePressed(MouseEvent e) 
    { 

    } 

    public void mouseReleased(MouseEvent e) 
    {   
    } 
    public void paint(Graphics g) 
    { 
      //x = getX(); 
      //y = getY(); 

      if(cropped) 
      { 
       g.drawImage(crop,200,200, 100, 100, this); 

      } 
      else 
      { 
       g.drawImage(img,100,100, 880, 660, this); 
      } 

     } 

    } 
相关问题