使用Java applet裁剪从Webcam获取的图像

时间:2012-10-16 14:30:46

标签: java image applet resizable

我的要求如下。

  

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

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

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

3 个答案:

答案 0 :(得分: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上的裁剪,但您可以保留矩形直到使用其他内容,例如双击......

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

你只需要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);
            }

        }

    }