Thứ Bảy, 6 tháng 12, 2014

Resize Image Java

public static void main(String[] args) throws IOException {
        String path = "E:\\xampp\\htdocs\\khoan.com\\static\\image\\product\\thumb\\";
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        System.out.println("Total No of Files:" + listOfFiles.length);
        Image img = null;
        BufferedImage tempPNG = null;
        BufferedImage tempJPG = null;
        File newFilePNG = null;
        File newFileJPG = null;
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                System.out.println("File " + listOfFiles[i].getName());
                img = ImageIO.read(new File(path + listOfFiles[i].getName()));
//                tempPNG = resizeImage(img, 100, 100);
                tempJPG = resizeImage(img, 225, 145);
//                newFilePNG = new File("/Users/pankaj/Desktop/images/resize/" + listOfFiles[i].getName() + "_New.png");
                newFileJPG = new File("E:\\xampp\\htdocs\\khoan.com\\static\\image\\product\\thumb_" + listOfFiles[i].getName());
//                ImageIO.write(tempPNG, "png", newFilePNG);
                ImageIO.write(tempJPG, "jpg", newFileJPG);
            }
        }
        System.out.println("DONE");
    }

    public static BufferedImage resizeImage(final Image image, int width, int height) {
        final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        final Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setComposite(AlphaComposite.Src);
        //below three lines are for RenderingHints for better image quality at cost of higher processing time
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        graphics2D.drawImage(image, 0, 0, width, height, null);
        graphics2D.dispose();
        return bufferedImage;
    }

Không có nhận xét nào:

Đăng nhận xét