Java动态调整基于给定输入像素的图像

时间:2014-07-25 10:51:50

标签: java html image pixel

我想通过从外面明确地给出像素来调整图像的大小。这里我能够通过指定压缩质量来通过硬编码来压缩图像。

我需要通过明确的高度和宽度来调整大小

在这里,我使用以下代码压缩图像:

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ImageServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        File f = new File("D:\\Compress");
        File f1 = new File("D:\\Original");
        if (!f.exists()) {
            f.mkdir();
        }
        if (!f1.exists()) {
            f1.mkdir();
        }
        String p = req.getParameter("myfile");
        String actualPath = "D:/Original/" + p;
        /*
         * System.out.println(p); File file = new File(p); String s=
         * file.getCanonicalPath(); System.out.println(s);
         */
        File imageFile = new File(actualPath);
        File compressedImageFile = new File("D://Compress/compressed_" + p + ".jpg");
        InputStream is = new FileInputStream(imageFile);
        OutputStream os = new FileOutputStream(compressedImageFile);
        float quality = 0.3f;

        // create a BufferedImage as the result of decoding the supplied
        // InputStream
        //reads image
        BufferedImage image = ImageIO.read(is);

        // get all image writers for JPG format
        Iterator writers = (Iterator) ImageIO.getImageWritersByFormatName("jpg");

        // checks the format
        java.util.Iterator<ImageWriter> iterator = (java.util.Iterator<ImageWriter>) writers;
        if (!iterator.hasNext())
            throw new IllegalStateException("No writers found");


        ImageWriter writer = (ImageWriter) ((java.util.Iterator<ImageWriter>) writers).next();
        //sends to output stream
        ImageOutputStream ios = ImageIO.createImageOutputStream(os);
        writer.setOutput(ios);
        ImageWriteParam param = writer.getDefaultWriteParam();

        // compress to a given quality
        param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        param.setCompressionQuality(quality);

        // appends a complete image stream containing a single image and
        // associated stream and image metadata and thumbnails to the output
        writer.write(null, new IIOImage(image, null, null), param);

        // close all streams
        is.close();
        os.close();
        ios.close();
        writer.dispose();

        // redirect to success page
        resp.sendRedirect("./Success.html");

    }

}

因此编码此工作正常,但它只调整大小(意味着将43kb图像文件转换为23kb)图像大小。 在这里,我想改变图像的高度和宽度。

提前致谢。

0 个答案:

没有答案