java圖片裁剪和java生成縮略圖


一、縮略圖

在瀏覽相冊的時候。可能須要生成相應的縮略圖。


直接上代碼:

public class ImageUtil {

    private Logger log = LoggerFactory.getLogger(getClass());

    private static String DEFAULT_PREVFIX = "thumb_";
    private static Boolean DEFAULT_FORCE = false;//建議該值為false

    /** * <p>Title: thumbnailImage</p> * <p>Description: 依據圖片路徑生成縮略圖 </p> * @param imagePath 原圖片路徑 * @param w 縮略圖寬 * @param h 縮略圖高 * @param prevfix 生成縮略圖的前綴 * @param force 是否強制依照寬高生成縮略圖(假設為false,則生成最佳比例縮略圖) */
    public void thumbnailImage(String imagePath, int w, int h, String prevfix, boolean force){
        File imgFile = new File(imagePath);
        if(imgFile.exists()){
            try {
                // ImageIO 支持的圖片類型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG, JPEG, WBMP, GIF, gif]
                String types = Arrays.toString(ImageIO.getReaderFormatNames());
                String suffix = null;
                // 獲取圖片后綴
                if(imgFile.getName().indexOf(".") > -1) {
                    suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
                }// 類型和圖片后綴所有小寫,然后推斷后綴是否合法
                if(suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0){
                    log.error("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
                    return ;
                }
                log.debug("target image's size, width:{}, height:{}.",w,h);
                Image img = ImageIO.read(imgFile);
                if(!force){
                    // 依據原圖與要求的縮略圖比例,找到最合適的縮略圖比例
                    int width = img.getWidth(null);
                    int height = img.getHeight(null);
                    if((width*1.0)/w < (height*1.0)/h){
                        if(width > w){
                            h = Integer.parseInt(new java.text.DecimalFormat("0").format(height * w/(width*1.0)));
                            log.debug("change image's height, width:{}, height:{}.",w,h);
                        }
                    } else {
                        if(height > h){
                            w = Integer.parseInt(new java.text.DecimalFormat("0").format(width * h/(height*1.0)));
                            log.debug("change image's width, width:{}, height:{}.",w,h);
                        }
                    }
                }
                BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
                Graphics g = bi.getGraphics();
                g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
                g.dispose();
                String p = imgFile.getPath();
                // 將圖片保存在原文件夾並加上前綴
                ImageIO.write(bi, suffix, new File(p.substring(0,p.lastIndexOf(File.separator)) + File.separator + prevfix +imgFile.getName()));
                log.debug("縮略圖在原路徑下生成成功");
            } catch (IOException e) {
               log.error("generate thumbnail image failed.",e);
            }
        }else{
            log.warn("the image is not exist.");
        }
    }

    public static void main(String[] args) {
        new ImageUtil().thumbnailImage("C:/Users/cm/Desktop/我的頁面/images/girlNoImg.jpg", 100, 150,DEFAULT_PREVFIX,DEFAULT_FORCE);
    }
}

直接執行main方法,填入相相應的參數就可以。

二、配合 js 生成裁剪圖片

在我們改動個人微博、qq資料的時候能夠上傳個人頭像,並能夠剪裁個人頭像然后上傳。剪裁圖片的大小樣式是通過javascript實現的,可是它並不能生成一個新的圖片。可是js剪裁圖片提供圖片的x,y坐標以及寬高,通過這四個參數我們能夠依據原圖片生成新的剪裁圖片。
步驟:
1、首先通過頁面利用js實現圖片剪切瀏覽功能。我參照慕課網提供的資料並略微更改了一下。另外我們也能夠使用插件,比方Jcrop是款非常不錯的圖片裁剪插件。

下載地址http://download.csdn.net/detail/u012385190/9733480

這里寫圖片描寫敘述
最后效果圖如上,左側能夠拖動拖拉,右側是預覽圖。


2、java生成並保存剪切圖片

public class ImageUtil2 {

    private Logger log = LoggerFactory.getLogger(getClass());

    private static String DEFAULT_CUT_PREVFIX = "cut_";

    /** * Description: 依據原圖與裁切size截取局部圖片 * @param srcImg 源圖片 * @param output 圖片輸出流 * @param rect 須要截取部分的坐標和大小 */
    public void cutImage(File srcImg, OutputStream output,java.awt.Rectangle rect) {
        if (srcImg.exists()) {
            java.io.FileInputStream fis = null;
            ImageInputStream iis = null;
            try {
                fis = new FileInputStream(srcImg);
                // ImageIO 支持的圖片類型 : [BMP, bmp, jpg, JPG, wbmp, jpeg, png, PNG,
                // JPEG, WBMP, GIF, gif]
                String types = Arrays.toString(ImageIO.getReaderFormatNames())
                        .replace("]", ",");
                String suffix = null;
                // 獲取圖片后綴
                if (srcImg.getName().indexOf(".") > -1) {
                    suffix = srcImg.getName().substring(srcImg.getName().lastIndexOf(".") + 1);
                }// 類型和圖片后綴所有小寫。然后推斷后綴是否合法
                if (suffix == null
                        || types.toLowerCase().indexOf(suffix.toLowerCase() + ",") < 0) {
                    log.error("Sorry, the image suffix is illegal. the standard image suffix is {}."+ types);
                    return;
                }
                // 將FileInputStream 轉換為ImageInputStream
                iis = ImageIO.createImageInputStream(fis);
                // 依據圖片類型獲取該種類型的ImageReader
                ImageReader reader = ImageIO.getImageReadersBySuffix(suffix).next();
                reader.setInput(iis, true);
                ImageReadParam param = reader.getDefaultReadParam();
                param.setSourceRegion(rect);
                BufferedImage bi = reader.read(0, param);
                ImageIO.write(bi, suffix, output);
                log.info("圖片生成成功,請到文件夾下查看");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fis != null)
                        fis.close();
                    if (iis != null)
                        iis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {
            log.warn("the src image is not exist.");
        }
    }


    //生成目標文件路徑
    public void cutImage(File srcImg, String destImgPath,java.awt.Rectangle rect) {
        File destImg = new File(destImgPath);
        if (destImg.exists()) {
            String p = destImg.getPath();
            try {
                if (!destImg.isDirectory())
                    p = destImg.getParent();
                if (!p.endsWith(File.separator))
                    p = p + File.separator;
                cutImage(srcImg,new java.io.FileOutputStream(p + DEFAULT_CUT_PREVFIX+ "_"+ srcImg.getName()), rect);
            } catch (FileNotFoundException e) {
                log.warn("the dest image is not exist.");
            }
        } else
            log.warn("the dest image folder is not exist.");
    }


    public void cutImage(String srcImg, String destImg, int x, int y, int width,
            int height) {
        cutImage(new File(srcImg), destImg, new java.awt.Rectangle(x, y, width, height));
    }


    public static void main(String[] args) {
        new ImageUtil2().cutImage("C:/Users/cm/Desktop/我的頁面/images/boyNoImg.jpg", "C:/Users/cm/Desktop/我的頁面/images/imgs",0, 0, 61, 166);
        //new ImageUtil2().cutImage("C:/Users/cm/Desktop/Jcrop-master/demos/demo_files/sago.jpg", "C:/Users/cm/Desktop/我的頁面/images/imgs",124, 110, 196, 176);
    }
}

該方法能夠直接在main里執行。

傳入的四個參數分別為圖像路徑、left值、top值,長度、寬度。

以下分析一下怎么獲取這四個參數:
1、以我的js剪切為例,通過F12獲得例如以下:
這里寫圖片描寫敘述
圖片中紅色部分就是圖片剪切的div。我們能夠通過拖拽剪裁區大小寬度等來觀察改變了哪些參數,然后確定詳細相應的參數值分別相應哪個參數值。如圖片中我的x/y/width/height分別為40,28,224,228。

注意:在js中我將該div和圖片的長寬都定義為300*300,為配合測試所以我下載的圖片也為300*300。假設你測試的圖片大小不為300*300。那么你直接在上面java中測試的效果會和你前端看到的不一樣,由於你前端的圖片寬高我定義為300*300。而你實際圖片(即java中的圖片)不是。

那么這個問題假設處理呢?

在你的java代碼中獲取原圖片的長寬,然后推斷原圖片的長寬是不是300*300,假設不是就生成該圖片的300*300的縮略圖,然后將該300*300的縮略圖作為裁剪的圖片原型。

(我的代碼中沒有處理,須要的自己處理,用完圖片之后刪除縮略圖)

2、Jcrop獲取參數
這里寫圖片描寫敘述


如圖,Jcrop直接提供了參數,能夠直接使用。可是它有個缺點就是在前端頁面的圖片大小區域不固定,假設你有個大像素圖片,那么會非常丑,比方我在相應文件中有個圖片soga_bak.jpg,換成這個圖片就不好了。

所以綜上建議用第一個js。然后推斷原圖片的長寬是不是300。不是的生成300*300縮略圖。然后將縮略圖作為裁剪原型圖,用完了再刪除縮略圖。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM