Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enhanced Image Cropping

Which is best java library to do cropping.The aim is to maintain the clarity of the cropped image at 2x zoom.The sides of cropped image are getting blurred while zooming to 2X level.I tried Java advanced imaging(JAI) to crop with rendering hints.I also tried RescaleDescriptor(JAI) to enhance the image.But it works only for a black particle in a white background.Is there any other libraries which i can use?

like image 810
aruns Avatar asked May 09 '11 12:05

aruns


2 Answers

Using Thumbnailator, one can crop the original image and zoom into that region with the following code:

Thumbnails.of("/path/to/image")
    .sourceRegion(Positions.CENTER, 100, 100)
    .scale(2.0)
    .resizer(Resizers.BICUBIC)
    .toFile("/path/to/cropped+zoomed-image");

The above will take the central 100 px by 100 px region of the source image and enlarge it by a scaling factor of 2.0x using bicubic interpolation to result in a smooth image without jagged edges:

Source image (left) cropped and enlarged by Thumbnailator (right)
(source: coobird.net)

Although Thumbnailator is a library which specializes in creating thumbnails (hence the name), it can usually be adapted to create enlarged images as well.

Full disclosure: I am the developer of Thumbnailator.

like image 151
coobird Avatar answered Nov 15 '22 12:11

coobird


Before using external libraries try setting these rendering hints (for "normal" swing rendering):

g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                   RenderingHints.VALUE_INTERPOLATION_BILINEAR); // or .._BICUBIC

g.setRenderingHint(RenderingHints.KEY_RENDERING,
                   RenderingHints.VALUE_RENDER_QUALITY);

g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                   RenderingHints.VALUE_ANTIALIAS_ON);

The code below produces this screenshot:

screenshot

public static void main(String[] args) throws IOException {
    BufferedImage o = ImageIO.read(new URL("http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png"));
    final BufferedImage image = o.getSubimage(220, 220, 80, 80);
    final int width  = image.getWidth() * 4;
    final int height = image.getHeight() * 4;

    JFrame frame = new JFrame("Test");
    frame.setLayout(new GridLayout(1, 2));

    frame.add(new JComponent() {
        public void paintComponent(Graphics g) {
            Graphics2D g2d = (Graphics2D) g;
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                 RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                                 RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                 RenderingHints.VALUE_ANTIALIAS_ON);

            g2d.drawImage(image, 0, 0, width, height, null);
        }
    });
    frame.add(new JComponent() {
        public void paintComponent(Graphics g) {
            g.drawImage(image, 0, 0, width, height, null);
        }
    });

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(649, 351);
    frame.setVisible(true);
}
like image 34
dacwe Avatar answered Nov 15 '22 11:11

dacwe