Java BufferedImage from URL urlToJpegThumbnail(String url, String filename, double maxWidth, double maxHeight)

Here you can find the source of urlToJpegThumbnail(String url, String filename, double maxWidth, double maxHeight)

Description

Uses HtmlDoc command line tool, pipes an HTML URL to GhostScript which outputs a JPEG file with the given dimensions.

License

Open Source License

Parameter

Parameter Description
url Description of the Parameter
filename Description of the Parameter
maxWidth Description of the Parameter
maxHeight Description of the Parameter

Return

Description of the Return Value

Declaration

public static int urlToJpegThumbnail(String url, String filename, double maxWidth, double maxHeight) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.imageio.ImageIO;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;

import java.io.File;

public class Main {
    /**/*from  w w  w .j a v a 2s.c om*/
     * Uses HtmlDoc command line tool, pipes an HTML URL to GhostScript
     * which outputs a JPEG file with the given dimensions.
     *
     * @param url       Description of the Parameter
     * @param filename  Description of the Parameter
     * @param maxWidth  Description of the Parameter
     * @param maxHeight Description of the Parameter
     * @return Description of the Return Value
     */
    public static int urlToJpegThumbnail(String url, String filename, double maxWidth, double maxHeight) {
        // Determine paths
        File gsPath = new File("/usr/bin/gs");
        if (!gsPath.exists()) {
            gsPath = new File("/sw/bin/gs");
        }
        Process process;
        Runtime runtime;
        java.io.InputStream input;
        String command[] = { "/bin/sh", "-c",
                "/usr/bin/htmldoc --quiet --jpeg " + "--webpage -t ps --left 0 --top 0 "
                        + "--header ... --footer ... --landscape " + url + " " + "| " + gsPath.getAbsoluteFile()
                        + " -q -sDEVICE=jpeg -dNOPAUSE -dBATCH -sOutputFile=- -" };
        runtime = Runtime.getRuntime();
        try {
            process = runtime.exec(command);
            input = process.getInputStream();
            BufferedImage originalImage = ImageIO.read(input);

            //Calculate scaling
            double ratioWidth = maxWidth / originalImage.getWidth();
            double ratioHeight = maxHeight / originalImage.getHeight();
            double ratio = 1;
            if (maxWidth > 0 && maxHeight < 0) {
                ratio = ratioWidth;
            } else if (maxHeight > 0 && maxWidth < 0) {
                ratio = ratioHeight;
            } else {
                if (ratioWidth < ratioHeight) {
                    ratio = ratioWidth;
                } else {
                    ratio = ratioHeight;
                }
            }
            //Scale
            AffineTransform at = new AffineTransform();
            at.scale(ratio, ratio);
            AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            BufferedImage scaledImage = op.filter(originalImage, null);

            //Rotate
            at = new AffineTransform();
            at.setToTranslation(scaledImage.getHeight() / 2, scaledImage.getWidth() / 2);
            at.rotate(Math.toRadians(90));
            at.translate(-(scaledImage.getWidth() / 2), -(scaledImage.getHeight() / 2));
            op = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            BufferedImage thumbnailImage = op.filter(scaledImage, null);

            File thumbnailFile = new File(filename);
            ImageIO.write(thumbnailImage, "jpg", thumbnailFile);
            return (process.waitFor());
        } catch (Exception e) {
            e.printStackTrace(System.out);
            System.err.println("GraphicUtils-> urlToJpeg error: " + e.toString());
            return (1);
        }
    }

    /**
     * Uses HtmlDoc command line tool, pipes an HTML URL to GhostScript
     * which outputs a JPEG file.
     *
     * @param url      Description of the Parameter
     * @param filename Description of the Parameter
     * @return Description of the Return Value
     */
    public static int urlToJpegThumbnail(String url, String filename) {
        Process process;
        Runtime runtime;
        java.io.InputStream input;
        String command[] = { "/bin/sh", "-c",
                "/usr/bin/htmldoc --quiet --jpeg " + "--webpage -t ps --left 0 --top 0 "
                        + "--header ... --footer ... " + url + " "
                        + "| /usr/bin/gs -q -sDEVICE=jpeg -dNOPAUSE -dBATCH -sOutputFile=- -" };
        runtime = Runtime.getRuntime();
        try {
            process = runtime.exec(command);
            input = process.getInputStream();
            BufferedImage originalImage = ImageIO.read(input);
            File thumbnailFile = new File(filename);
            ImageIO.write(originalImage, "jpg", thumbnailFile);
            return (0);
        } catch (Exception e) {
            e.printStackTrace(System.out);
            System.err.println("GraphicUtils-> urlToJpeg error: " + e.toString());
            return (1);
        }
    }
}

Related

  1. getFileAndDownload(String urlString, String folder)
  2. httpGetImage(String url)
  3. Img2ByteByUrl(String strUrl)
  4. urlToImage(String urlstring)
  5. urlToImage(URL imageUrl)