How do I crop an image in Java?

前端 未结 7 1302
逝去的感伤
逝去的感伤 2020-11-29 00:22

I want to crop an image manually using the mouse.
Suppose the image has some text, and I want to select some text from an image, then for that purpose I want to crop th

相关标签:
7条回答
  • 2020-11-29 00:37

    This question has not enough information to answer. A general solution (depending on your GUI framework): add a mouse event handler that will catch clicks and mouse movements. This will give you your (x, y) coordinates. Next use these coordinates to crop your image.

    0 讨论(0)
  • 2020-11-29 00:41

    The solution I found most useful for cropping a buffered image uses the getSubImage(x,y,w,h);

    My cropping routine ended up looking like this:

      private BufferedImage cropImage(BufferedImage src, Rectangle rect) {
          BufferedImage dest = src.getSubimage(0, 0, rect.width, rect.height);
          return dest; 
       }
    
    0 讨论(0)
  • 2020-11-29 00:45

    You need to read about Java Image API and mouse-related API, maybe somewhere under the java.awt.event package.

    For a start, you need to be able to load and display the image to the screen, maybe you'll use a JPanel.

    Then from there, you will try implement a mouse motion listener interface and other related interfaces. Maybe you'll get tied on the mouseDragged method...

    For a mousedragged action, you will get the coordinate of the rectangle form by the drag...

    Then from these coordinates, you will get the subimage from the image you have and you sort of redraw it anew....

    And then display the cropped image... I don't know if this will work, just a product of my imagination... just a thought!

    0 讨论(0)
  • 2020-11-29 00:52

    I'm giving this example because this actually work for my use case.

    I was trying to use the AWS Rekognition API. The API returns a BoundingBox object:

    BoundingBox boundingBox = faceDetail.getBoundingBox();
    

    The code below uses it to crop the image:

    import com.amazonaws.services.rekognition.model.BoundingBox;
    
    private BufferedImage cropImage(BufferedImage image, BoundingBox box) {
            Rectangle goal = new Rectangle(Math.round(box.getLeft()* image.getWidth()),Math.round(box.getTop()* image.getHeight()),Math.round(box.getWidth() * image.getWidth()), Math.round(box.getHeight() * image.getHeight()));
    
            Rectangle clip = goal.intersection(new Rectangle(image.getWidth(), image.getHeight()));
    
            BufferedImage clippedImg = image.getSubimage(clip.x, clip.y , clip.width, clip.height);
    
            return clippedImg;
        }
    
    0 讨论(0)
  • 2020-11-29 00:54

    This is a method which will work:

    import java.awt.image.BufferedImage;
    import java.awt.Rectangle;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public BufferedImage crop(BufferedImage src, Rectangle rect)
    {
        BufferedImage dest = new BufferedImage(rect.getWidth(), rect.getHeight(), BufferedImage.TYPE_ARGB_PRE);
        Graphics g = dest.getGraphics();
        g.drawImage(src, 0, 0, rect.getWidth(), rect.getHeight(), rect.getX(), rect.getY(), rect.getX() + rect.getWidth(), rect.getY() + rect.getHeight(), null);
        g.dispose();
        return dest;
    }
    

    Of course you have to make your own JComponent:

    import java.awt.event.MouseListener;
    import java.awt.event.MouseMotionListener;
    import java.awt.image.BufferedImage;
    import java.awt.Rectangle;
    import java.awt.Graphics;
    import javax.swing.JComponent;
    
    public class JImageCropComponent extends JComponent implements MouseListener, MouseMotionListener
    {
       private BufferedImage img;
       private int x1, y1, x2, y2;
    
       public JImageCropComponent(BufferedImage img)
       {
           this.img = img;
           this.addMouseListener(this);
           this.addMouseMotionListener(this);
       }
    
       public void setImage(BufferedImage img)
       {
           this.img = img;
       }
    
       public BufferedImage getImage()
       {
           return this;
       }
    
       @Override
       public void paintComponent(Graphics g)
       {
          g.drawImage(img, 0, 0, this);
          if (cropping)
          {
              // Paint the area we are going to crop.
              g.setColor(Color.RED);
              g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2));
          }
       }
    
       @Override
       public void mousePressed(MouseEvent evt)
       {
           this.x1 = evt.getX();
           this.y1 = evt.getY();
       }
    
       @Override
       public void mouseReleased(MouseEvent evt)
       {
           this.cropping = false;
           // Now we crop the image;
           // This is the method a wrote in the other snipped
           BufferedImage cropped = crop(new Rectangle(Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2));
           // Now you have the cropped image;
           // You have to choose what you want to do with it
           this.img = cropped;
       }
    
       @Override
       public void mouseDragged(MouseEvent evt)
       {
           cropping = true;
           this.x2 = evt.getX();
           this.y2 = evt.getY();
       }
    
       //TODO: Implement the other unused methods from Mouse(Motion)Listener
    
    }
    

    I didn't test it. Maybe there are some mistakes (I'm not sure about all the imports).

    You can put the crop(img, rect) method in this class. Hope this helps.

    0 讨论(0)
  • 2020-11-29 00:54
    File fileToWrite = new File(filePath, "url");
    
    BufferedImage bufferedImage = cropImage(fileToWrite, x, y, w, h);
    
    private BufferedImage cropImage(File filePath, int x, int y, int w, int h){
    
        try {
            BufferedImage originalImgage = ImageIO.read(filePath);
    
            BufferedImage subImgage = originalImgage.getSubimage(x, y, w, h);
    
    
    
            return subImgage;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    0 讨论(0)
提交回复
热议问题