package com.ying.util;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.*;
/**
* 裁剪、缩放图片工具类
*
*/
public class ImgUtils {
static BASE64Encoder encoder = new BASE64Encoder();
static BASE64Decoder decoder = new BASE64Decoder();
/**
* 图片转换为base64
* @param path
* @return
*/
public static String getImageBinary(String path) {
File f = new File(path);
try {
BufferedImage bi = ImageIO.read(f);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* base64转换为图片
* @param base64String
* @param outPath
*/
public static void base64StringToImage(String base64String, String outPath) {
try {
byte[] bytes1 = decoder.decodeBuffer(base64String);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi1 = ImageIO.read(bais);
File f1 = new File(outPath);
ImageIO.write(bi1, "jpg", f1);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* base64字符串转文件
* @param base64
* @return
*/
public static File base64ToFile(String base64,String path) {
File file = null;
FileOutputStream out = null;
try {
// 解码,然后将字节转换为文件
file = new File(path);
if (!file.exists())
file.createNewFile();
byte[] bytes = decoder.decodeBuffer(base64);// 将字符串转换为byte数组
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
byte[] buffer = new byte[1024];
out = new FileOutputStream(file);
int bytesum = 0;
int byteread = 0;
while ((byteread = in.read(buffer)) != -1) {
bytesum += byteread;
out.write(buffer, 0, byteread); // 文件写操作
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (out!= null) {
out.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
/**
* 缩放图片方法
* @param srcImageFile 要缩放的图片路径
* @param result 缩放后的图片路径
* @param height 目标高度像素
* @param width 目标宽度像素
* @param bb 是否补白
*/
public final static void scale(String srcImageFile, String result, int height, int width, boolean bb) {
try {
double ratio = 0.0; // 缩放比例
File f = new File(srcImageFile);
BufferedImage bi = ImageIO.read(f);
Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);//bi.SCALE_SMOOTH 选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
// 计算比例
if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
double ratioHeight = (new Integer(height)).doubleValue()/ bi.getHeight();
double ratioWhidth = (new Integer(width)).doubleValue()/ bi.getWidth();
if(ratioHeight>ratioWhidth){
ratio= ratioHeight;
}else{
ratio= ratioWhidth;
}
AffineTransformOp op = new AffineTransformOp(AffineTransform//仿射转换
.getScaleInstance(ratio, ratio), null);//返回表示剪切变换的变换
itemp = op.filter(bi, null);//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
}
if (bb) {//补白
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);//构造一个类型为预定义图像类型之一的 BufferedImage。
Graphics2D g = image.createGraphics();//创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
g.setColor(Color.white);//控制颜色
g.fillRect(0, 0, width, height);// 使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
if (width == itemp.getWidth(null))
g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
else
g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
itemp.getWidth(null), itemp.getHeight(null),
Color.white, null);
g.dispose();
itemp = image;
}
ImageIO.write((BufferedImage) itemp, "JPEG", new File(result)); //输出压缩图片
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 裁剪图片方法
* @param bufferedImage 图像源
* @param startX 裁剪开始x坐标
* @param startY 裁剪开始y坐标
* @param endX 裁剪结束x坐标
* @param endY 裁剪结束y坐标
* @return
*/
public static BufferedImage cropImage(BufferedImage bufferedImage, int startX, int startY, int endX, int endY) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
if (startX == -1) {
startX = 0;
}
if (startY == -1) {
startY = 0;
}
if (endX == -1) {
endX = width - 1;
}
if (endY == -1) {
endY = height - 1;
}
BufferedImage result = new BufferedImage(endX - startX, endY - startY, BufferedImage.TYPE_INT_RGB);
for (int x = startX; x < endX; ++x) {
for (int y = startY; y < endY; ++y) {
int rgb = bufferedImage.getRGB(x, y);
result.setRGB(x - startX, y - startY, rgb);
}
}
return result;
}
/**
* 裁剪图片方法
* @param base64 图像源base64
* @param startX 裁剪开始x坐标
* @param startY 裁剪开始y坐标
* @param endX 裁剪结束x坐标
* @param endY 裁剪结束y坐标
* @return
*/
public static String cropImage(String base64, int startX, int startY, int endX, int endY) {
try {
byte[] bytes1 = decoder.decodeBuffer(base64);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
BufferedImage bi = ImageIO.read(bais);
bi = ImgUtils.cropImage(bi, startX, startY, endX, endY);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);
byte[] bytes = baos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encodeBuffer(bytes).trim();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* BufferedImage 转 String
* @param bufferedImage
* @return
*/
public static String bufferedImageToString(BufferedImage bufferedImage){
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "jpg", baos);
byte[] bytes = baos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encodeBuffer(bytes).trim();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}