Java图片处理,压缩,旋转,Base64转码

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: 旋转图片到正常的方向.
*

    * @author qiyongkang

    * @since JDK 1.6

*/

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

//计算方向

        int orientation =getImageOrientation(file);

//      angle = 90d;

        if (orientation >ORIENTATION_ONE) {

//进行图片处理

            switch (orientation) {

case 3:

//需要旋转180度

                    angle =180d;

break;

case 6:

//需要旋转270度

                    angle =270d;

break;

case 8:

//需要旋转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();

}

}

/**

* 将网络图片进行Base64位编码

*

    * @param imgUrl

    *            图片的url路径,如http://.....xx.jpg

    * @return

    */

    public static String encodeImgageToBase64(URL imageUrl) {// 将图片文件转化为字节数组字符串,并对其进行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();

}

// 对字节数组Base64编码

        BASE64Encoder encoder =new BASE64Encoder();

return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串

    }

/**

* 将本地图片进行Base64位编码

*

    * @param imgUrl

    *            图片的url路径,如http://.....xx.jpg

    * @return

    */

    public static String encodeImgageToBase64(File imageFile) {// 将图片文件转化为字节数组字符串,并对其进行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();

}

// 对字节数组Base64编码

        BASE64Encoder encoder =new BASE64Encoder();

return encoder.encode(outputStream.toByteArray());// 返回Base64编码过的字节数组字符串

    }

/**

* 将Base64位编码的图片进行解码,并保存到指定目录

*

    * @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();

}

}

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、流的概念和作用。 流是一种有顺序的,有起点和终点的字节集合,是对数据传输的总成或抽象。即数据在两设备之间的传输...
    布鲁斯不吐丝阅读 13,400评论 2 95
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,899评论 18 399
  • 一、 1、请用Java写一个冒泡排序方法 【参考答案】 public static void Bubble(int...
    独云阅读 5,242评论 0 6
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,712评论 0 17
  • 【本文系王博华原创,转载请注明出处!】 昨天的文章总结了一下我写文章的收获,其实你不知道,写文章太苦了,每天要不停...
    王博华阅读 3,110评论 0 1