1. 程式人生 > >java對圖片進行操作,僅僅是小demo

java對圖片進行操作,僅僅是小demo

sed throw bytearray 路徑 設置圖 圖片大小 tag href 寬度

package com.cy.thumb;

import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator; import javax.imageio.ImageIO; import javax.imageio.ImageReadParam; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class Thumb { public
static void main(String[] args) throws IOException { //把圖片image.png 長寬等比縮小5倍。 reduceImage("E:/image.png", "E:/image1.png", 5); //把圖片image.png 長寬各設置為100 reduceImage("E:/image.png", "E:/image2.png", 100, 100); } /** * 對圖片進行剪裁 返回字節數組 *
@param is 圖片輸入流 * @param width 裁剪圖片的寬 * @param height 裁剪圖片的高 * @param imageFormat 輸出圖片的格式 "jpeg jpg等" * @return */ public static byte[] clipImage(InputStream is,int width, int height, String imageFormat){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { // 構造Image對象 BufferedImage src = javax.imageio.ImageIO.read(is); // 縮小邊長 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 繪制 縮小 後的圖片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); ImageIO.write(tag, imageFormat, bos); } catch (IOException e) { e.printStackTrace(); } return bos.toByteArray(); } /** * 重置圖片大小 * @param srcImagePath 讀取圖片路徑 * @param toImagePath 寫入圖片路徑 * @param width 重新設置圖片的寬 * @param height 重新設置圖片的高 * @throws IOException */ public static void reduceImage(String srcImagePath,String toImagePath,int width, int height) throws IOException{ FileOutputStream out = null; try{ //讀入文件 File file = new File(srcImagePath); // 構造Image對象 BufferedImage src = javax.imageio.ImageIO.read(file); // 縮小邊長 BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); // 繪制 縮小 後的圖片 tag.getGraphics().drawImage(src, 0, 0, width, height, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); }catch(Exception e){ e.printStackTrace(); }finally{ if(out != null){ out.close(); } } } /** * 按倍率縮小圖片 * @param srcImagePath 讀取圖片路徑 * @param toImagePath 寫入圖片路徑 * @param ratio 縮小比率 寬、高一起等比率縮小 * @throws IOException */ public static void reduceImage(String srcImagePath,String toImagePath,int ratio) throws IOException{ FileOutputStream out = null; try{ //讀入文件 File file = new File(srcImagePath); // 構造Image對象 BufferedImage src = javax.imageio.ImageIO.read(file); int width = src.getWidth(); int height = src.getHeight(); // 縮小邊長 BufferedImage tag = new BufferedImage(width / ratio, height / ratio, BufferedImage.TYPE_INT_RGB); // 繪制 縮小 後的圖片 tag.getGraphics().drawImage(src, 0, 0, width / ratio, height / ratio, null); out = new FileOutputStream(toImagePath); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(tag); }catch(Exception e){ e.printStackTrace(); }finally{ if(out != null){ out.close(); } } } /** * 對圖片裁剪,並把裁剪新圖片保存 * @param srcPath 讀取源圖片路徑 * @param toPath 寫入圖片路徑 * @param x 剪切起始點x坐標 * @param y 剪切起始點y坐標 * @param width 剪切寬度 * @param height 剪切高度 * @param readImageFormat 讀取圖片格式 * @param writeImageFormat 寫入圖片格式 */ public static void cropImage(String srcPath, String toPath, int x,int y,int width,int height, String readImageFormat,String writeImageFormat){ FileInputStream fis = null ; ImageInputStream iis =null ; try{ //讀取圖片文件 fis = new FileInputStream(srcPath); Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName(readImageFormat); ImageReader reader = readers.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)); }catch(Exception e){ e.printStackTrace(); }finally{ try{ if(fis!=null){ fis.close(); } if(iis!=null){ iis.close(); } }catch(Exception e){ e.printStackTrace(); } } } }

上面部分參考文章:http://blog.csdn.net/u012486840/article/details/52937823                           

下面轉載自:http://blog.csdn.net/u012481520/article/details/51802469#t0

Java之BufferedImage簡談

技術分享
 int width = 100;
 int height = 100;
  // 1.創建一個不帶透明色的BufferedImage對象
BufferedImage bimage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);

// 2.創建一個帶透明色的BufferedImage對象
 bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

// 3.創建一個與屏幕相適應的BufferedImage對象
。。。。

。。。。

四、BufferedImage  —->byte[]
ImageIO.write(BufferedImage image,String format,OutputStream out);方法可以很好的解決問題;
參數image表示獲得的BufferedImage;
參數format表示圖片的格式,比如“gif”等;
參數out表示輸出流,如果要轉成Byte數組,則輸出流為ByteArrayOutputStream即可;
執行完後,只需要toByteArray()就能得到byte[];


五、byte[] ——>BufferedImage
ByteArrayInputStream in = new ByteArrayInputStream(byte[]b);    //將b作為輸入流;
BufferedImage image = ImageIO.read(InputStream in);     //將in作為輸入流,讀取圖片存入image中,而這裏in可以為ByteArrayInputStream();
View Code

java對圖片進行操作,僅僅是小demo