开发者

Enhanced Image Cropping

开发者 https://www.devze.com 2023-03-04 11:51 出处:网络
Which is best java library to do开发者_StackOverflow中文版 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 2

Which is best java library to do开发者_StackOverflow中文版 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?


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:

Enhanced Image Cropping


(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.


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:

Enhanced Image Cropping

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);
}


I recommend ImageJ, a great library for image processing.

Here is a code example for cropping :

ImageProcessor ip = //... get the image processor to crop
ip.setRoi(left,top,width,height); //set the "Region Of Interest" (rectangle within the image) 
ImageProcessor ip2 = ip.crop();
0

精彩评论

暂无评论...
验证码 换一张
取 消