How do I crop an image in Java?

前端 未结 7 1333
逝去的感伤
逝去的感伤 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: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;
        }
    

提交回复
热议问题