简体   繁体   中英

Java crop specific part of different resolution images

I am looking to get a specific part of an image through java. I have 11 images with different resolutions.

If we take this as an example image

I want to extract this part

Here's my code

try {
        BufferedImage originalImgage = ImageIO.read(new File("bin\\" + backImage[randNum]));                    

        BufferedImage SubImgage = originalImgage.getSubimage((originalImgage.getWidth()/2) - 340 , originalImgage.getHeight() - 70, 700, 70);                   
        System.out.println("" + (originalImgage.getWidth()/2 - 700));
        ImageIO.write(SubImgage, "png", new File("bin\\gauLock.png"));                                  

    } catch (IOException e) {
        e.printStackTrace();
    }           

As you can see, the height is not a problem but the width is because of different resolutions

So my question is how can I obtain a specific part of all images

Think about the amount of the image you want as a fraction of the whole image. If you want the middle 50% of the image horizontally, this would work no matter the resolution:

    BufferedImage SubImgage = originalImgage.getSubimage((originalImgage.getWidth()/4), originalImgage.getHeight() - 70, originalImage.getWidth()/2, 70);
    System.out.println("" + (originalImgage.getWidth()/2 - 700));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM