java圖片處理

來源:互聯網
上載者:User

標籤:格式   array   nal   create   簡單的   pat   處理   gpo   logs   

Java中進行映像I/O(即讀圖片和寫圖片,不涉及到複雜影像處理)一個類包含靜態方便定位方法 ImageReaders和 ImageWriters,並進行簡單的編碼和解碼。

  • 基本代碼

    /*File f1 = new File("Z:\\java\\1.png");    BufferedImage image = ImageIO.read(f1);    File f2 = new File("Z:\\java\\2.tiff");    ImageIO.write(image, "png", f2);*///對應輸入圖片的格式
  • 參數化代碼

    @Test //擷取輸入輸出圖片格式    public void f1() {        String readFormats[] = ImageIO.getReaderFormatNames();        String writeFormats[] = ImageIO.getWriterFormatNames();        System.out.println(  Arrays.asList(readFormats));        System.out.println( Arrays.asList(writeFormats));    }        @Test    public void f2() {        Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName("png");    }     /**      * 對圖片裁剪,並把裁剪新圖片儲存      * @param srcPath 讀取源圖片路徑     * @param toPath    寫入圖片路徑     * @param x 剪下起始點x座標     * @param y 剪下起始點y座標     * @param width 剪下寬度     * @param height     剪下高度     * @param readImageFormat  讀取圖片格式     * @param writeImageFormat 寫入圖片格式     * @throws IOException     */    public void cropImage(String srcPath,String toPath,            int x,int y,int width,int height,            String readImageFormat,String writeImageFormat) throws IOException{           FileInputStream fis = null ;        ImageInputStream iis =null ;        try{               //讀取圖片檔案            fis = new FileInputStream(srcPath);             Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(readImageFormat);             ImageReader reader = (ImageReader) it.next();             //擷取圖片流             iis = ImageIO.createImageInputStream(fis);              reader.setInput(iis,true) ;            ImageReadParam param = reader.getDefaultReadParam();             //定義一個矩形            Rectangle rect = new Rectangle(x, y, width, height);             //提供一個 BufferedImage,將其用作解碼像素資料的目標。             param.setSourceRegion(rect);            BufferedImage bi = reader.read(0,param);                            //儲存新圖片             ImageIO.write(bi, writeImageFormat, new File(toPath));             }finally{            if(fis!=null)                fis.close();                   if(iis!=null)               iis.close();         }     }    

java圖片處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.