⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageprocessutil.java

📁 图片处理代码,可按定义尺寸保存新图片。图片处理代码,可按定义尺寸保存新图片。
💻 JAVA
字号:
package imageprocess;

import java.awt.image.BufferedImage;
import net.sourceforge.jiu.gui.awt.ImageCreator;
import net.sourceforge.jiu.geometry.ScaleReplication;
import net.sourceforge.jiu.gui.awt.ToolkitLoader;
import net.sourceforge.jiu.data.PixelImage;
import java.io.*;
import com.sun.image.codec.jpeg.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.util.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ImageProcessUtil {

    public static void main(String[] args) {
        boolean s = ImageProcessUtil.processImageUrl("http://210.192.102.22/rcbt/43b346b0020014fk.jpg",
                120);
        System.out.println(s);

    }
    static String tmpdir = System.getProperty("user.dir");
    public static boolean processImageUrl(String url, int smallImgWidth) {

        if (!(url.endsWith("jpg") || url.endsWith("gif") ||
              url.endsWith("bmp") || url.endsWith("png"))) {
            return false;
        }
        if (! (url.startsWith("http") || url.startsWith("HTTP")) ) {
            return false;
        }
        String tmpfileName = tmpdir + "/" + (new Date()).getTime() + ".jpg";
        File tmpFile = new File(tmpfileName);

        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(url);
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                                        new DefaultHttpMethodRetryHandler(3, false));

        try {
            int statusCode = client.executeMethod(method);
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("Method failed: " + method.getStatusLine());
            }
            // Read the response body.
            byte[] responseBody = method.getResponseBody();

            System.out.println("::::" + tmpFile);
            FileOutputStream fos = new FileOutputStream(tmpFile);
            fos.write(responseBody);
            fos.close();
            processImageFile(tmpfileName,smallImgWidth);
        } catch (HttpException e) {
            System.err.println("Fatal protocol violation: " + e.getMessage());
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            System.err.println("Fatal transport error: " + e.getMessage());
            e.printStackTrace();
            return false;
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
        return true;
    }


    public static boolean processImageFile(String picPath, int smallImgWidth) {
        File oriPicFile = new File(picPath);
        if (!oriPicFile.isFile()) {
            return false;
        }
        if (!(picPath.endsWith("jpg") || picPath.endsWith("gif") ||
              picPath.endsWith("bmp") || picPath.endsWith("png"))) {
            return false;
        }
        PixelImage image = ToolkitLoader.loadViaToolkitOrCodecs(picPath);
        try {

            ScaleReplication scale = new ScaleReplication();
            scale.setInputImage(image);
            int smallImgHeight = getSmallImgHeight(image.getWidth(),
                    image.getHeight(),
                    smallImgWidth);

            scale.setSize(smallImgWidth, smallImgHeight);
            scale.process();
            PixelImage scaledImage = scale.getOutputImage();

            String outFileName = picPath.substring(0, picPath.lastIndexOf('.'));
            String newJpgFileName = outFileName + ".jpg";
            String newSmallJpgFileName = outFileName + "_small.jpg";
            System.out.println(newJpgFileName);

            BufferedImage awtImage = ImageCreator.convertToAwtBufferedImage(
                    scaledImage);
            FileOutputStream out = new FileOutputStream(newSmallJpgFileName);
            JPEGEncodeParam jp = JPEGCodec.getDefaultJPEGEncodeParam(awtImage);
            jp.setQuality(0.91f, true);
            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out, jp);
            encoder.encode(awtImage);
            out.close();

            awtImage = ImageCreator.convertToAwtBufferedImage(
                    image);
            out = new FileOutputStream(newJpgFileName);
            jp = JPEGCodec.getDefaultJPEGEncodeParam(awtImage);
            jp.setQuality(0.80f, true);
            encoder = JPEGCodec.createJPEGEncoder(out, jp);
            encoder.encode(awtImage);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

        return true;
    }

    private static int getSmallImgHeight(int oriWidth, int oriHeight,
                                         int smallImgWidth) {
        double width = oriWidth;
        double height = oriHeight;
        double h = (height / width) * smallImgWidth;
        return new Double(h).intValue();
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -