Java圖片處理,壓縮,旋轉(zhuǎn),Base64轉(zhuǎn)碼

package com.zsx.servlets;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.drew.imaging.ImageMetadataReader;

import com.drew.metadata.Directory;

import com.drew.metadata.Metadata;

import com.drew.metadata.exif.ExifIFD0Directory;

import net.coobird.thumbnailator.Thumbnails;

import java.awt.image.BufferedImage;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.MalformedURLException;

import java.net.URL;

import javax.imageio.ImageIO;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

public class ImageUtil {

//寬

? ? public static int IMAGE_WIDTH =1;

//高

? ? public static int IMAGE_HEIGHT =2;

//方向

? ? public static int ORIENTATION_ONE =1;//正常

? ? public static int ORIENTATION_THREE =3;//180

? ? public static int ORIENTATION_SIX =6;//順時針90

? ? public static int ORIENTATION_EIGHT =8;//逆時針90,順時針270

/**

*

? ? * getImageSize: 獲取圖片大小.
*

? ? * @author qiyongkang

? ? * @return

? ? * @since JDK 1.6

*/

? ? public static int getImageSize(File file,int flag) {

int size =0;

if (file ==null || !file.exists())

return size;

try {

if (IMAGE_WIDTH == flag) {

size = ImageIO.read(file).getWidth();

}else if (IMAGE_HEIGHT == flag) {

size = ImageIO.read(file).getHeight();

}

}catch (IOException e) {

e.printStackTrace();

}

return size;

}

/**

*

? ? * getImageOrientation: 獲取圖片的方向
*

? ? * @author qiyongkang

? ? * @param file

? ? * @return

? ? * @since JDK 1.6

*/

? ? public static int getImageOrientation(File file) {

int orientation =ORIENTATION_ONE;

try {

Metadata metadata = ImageMetadataReader.readMetadata(file);

Directory dr = metadata.getDirectory(ExifIFD0Directory.class);

if (dr ==null) {

return orientation;

}

orientation = dr.getInt(ExifIFD0Directory.TAG_ORIENTATION);

}catch (Exception e) {

e.printStackTrace();

}

return orientation;

}

/**

*

? ? * rotateImage: 旋轉(zhuǎn)圖片到正常的方向.
*

? ? * @author qiyongkang

? ? * @since JDK 1.6

*/

? ? public static void rotateImage(File file,double angle) {

//計算方向

? ? ? ? int orientation =getImageOrientation(file);

//? ? ? angle = 90d;

? ? ? ? if (orientation >ORIENTATION_ONE) {

//進(jìn)行圖片處理

? ? ? ? ? ? switch (orientation) {

case 3:

//需要旋轉(zhuǎn)180度

? ? ? ? ? ? ? ? ? ? angle =180d;

break;

case 6:

//需要旋轉(zhuǎn)270度

? ? ? ? ? ? ? ? ? ? angle =270d;

break;

case 8:

//需要旋轉(zhuǎn)90度

? ? ? ? ? ? ? ? ? ? angle =90d;

break;

}

}

System.out.println("orientation="+orientation+"angle="+angle);

try {

Thumbnails.of(file).scale(1).rotate(angle).toFile(file);;

}catch (IOException e) {

e.printStackTrace();

}

}

/**

*

? ? * impressImage: 壓縮圖片.
*

? ? * @author qiyongkang

? ? * @param file

? ? * @param width

? ? * @param height

? ? * @since JDK 1.6

*/

? ? public static void impressImage(File file,int width,int height) {

try {

Thumbnails.of(file).size(width, height).toFile(file);

}catch (IOException e) {

e.printStackTrace();

}

}

/**

* 將網(wǎng)絡(luò)圖片進(jìn)行Base64位編碼

*

? ? * @param imgUrl

? ? *? ? ? ? ? ? 圖片的url路徑,如http://.....xx.jpg

? ? * @return

? ? */

? ? public static String encodeImgageToBase64(URL imageUrl) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理

? ? ? ? ByteArrayOutputStream outputStream =null;

try {

BufferedImage bufferedImage = ImageIO.read(imageUrl);

outputStream =new ByteArrayOutputStream();

ImageIO.write(bufferedImage,"jpg", outputStream);

}catch (MalformedURLException e1) {

e1.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}

// 對字節(jié)數(shù)組Base64編碼

? ? ? ? BASE64Encoder encoder =new BASE64Encoder();

return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串

? ? }

/**

* 將本地圖片進(jìn)行Base64位編碼

*

? ? * @param imgUrl

? ? *? ? ? ? ? ? 圖片的url路徑,如http://.....xx.jpg

? ? * @return

? ? */

? ? public static String encodeImgageToBase64(File imageFile) {// 將圖片文件轉(zhuǎn)化為字節(jié)數(shù)組字符串,并對其進(jìn)行Base64編碼處理

? ? ? ? ByteArrayOutputStream outputStream =null;

try {

BufferedImage bufferedImage = ImageIO.read(imageFile);

outputStream =new ByteArrayOutputStream();

ImageIO.write(bufferedImage,"jpg", outputStream);

}catch (MalformedURLException e1) {

e1.printStackTrace();

}catch (IOException e) {

e.printStackTrace();

}

// 對字節(jié)數(shù)組Base64編碼

? ? ? ? BASE64Encoder encoder =new BASE64Encoder();

return encoder.encode(outputStream.toByteArray());// 返回Base64編碼過的字節(jié)數(shù)組字符串

? ? }

/**

* 將Base64位編碼的圖片進(jìn)行解碼,并保存到指定目錄

*

? ? * @param base64

? ? *? ? ? ? ? ? base64編碼的圖片信息

? ? * @return

? ? */

? ? public static void decodeBase64ToImage(String base64, String path,

String imgName) {

BASE64Decoder decoder =new BASE64Decoder();

try {

FileOutputStream write =new FileOutputStream(new File(path

+ imgName));

byte[] decoderBytes = decoder.decodeBuffer(base64);

write.write(decoderBytes);

write.close();

}catch (IOException e) {

e.printStackTrace();

}

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、流的概念和作用。 流是一種有順序的,有起點和終點的字節(jié)集合,是對數(shù)據(jù)傳輸?shù)目偝苫虺橄蟆<磾?shù)據(jù)在兩設(shè)備之間的傳輸...
    布魯斯不吐絲閱讀 10,310評論 2 95
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,625評論 18 399
  • 一、 1、請用Java寫一個冒泡排序方法 【參考答案】 public static void Bubble(int...
    獨云閱讀 1,494評論 0 6
  • ¥開啟¥ 【iAPP實現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,295評論 0 17
  • 【本文系王博華原創(chuàng),轉(zhuǎn)載請注明出處!】 昨天的文章總結(jié)了一下我寫文章的收獲,其實你不知道,寫文章太苦了,每天要不停...
    王博華閱讀 470評論 0 1

友情鏈接更多精彩內(nèi)容