Reputation: 53
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
Upvotes: 0
Views: 90
Reputation: 1410
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));
Upvotes: 0