3

我的要求如下。

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

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

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

4

3 回答 3

4

就像是...

在此处输入图像描述

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();
                }
            }
        }
    }
}

或者你可以有一个倒置的选择......

在此处输入图像描述

只需用这个替换选择油漆代码(在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,但您可以保留矩形,直到用户执行其他操作,例如双击...

于 2012-10-16T19:36:54.013 回答
1

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

BufferedImage newImage = image.getSubimage(x, y, width, height);
于 2012-10-16T15:26:18.633 回答
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);
            }

        }

    }
于 2014-03-21T05:40:37.600 回答