package com.basic.analy.utils;
|
|
import com.cloud.common.model.FileInfos;
|
import com.cloud.common.utils.FastDFSUtil;
|
import lombok.Data;
|
import lombok.extern.log4j.Log4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Component;
|
import javax.imageio.ImageIO;
|
import javax.imageio.stream.FileImageInputStream;
|
import java.awt.*;
|
import java.awt.image.BufferedImage;
|
import java.awt.image.CropImageFilter;
|
import java.awt.image.FilteredImageSource;
|
import java.awt.image.ImageFilter;
|
import java.io.ByteArrayOutputStream;
|
import java.io.File;
|
import java.io.FileNotFoundException;
|
import java.io.IOException;
|
import java.net.URL;
|
|
@Log4j
|
@Component
|
@Data
|
public class ImageUtils {
|
/**
|
* 几种常见的图片格式
|
*/
|
public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式
|
public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组
|
public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组
|
public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式
|
public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形
|
public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop
|
|
@Value("${imgPath.ip}")
|
private String httpImgUrl = "";
|
|
@Autowired
|
private FastDFSUtil fastDFSUtil;
|
|
public static void main(String[] args) {
|
// TODO Auto-generated method stub
|
|
}
|
|
/**
|
* 图像切割(按指定起点坐标和宽高切割)
|
* @param srcImageFile 源图像地址
|
* @param result 切片后的图像地址
|
* @param x 目标切片起点坐标X
|
* @param y 目标切片起点坐标Y
|
* @param width 目标切片宽度
|
* @param height 目标切片高度
|
*/
|
public final static byte[] cut(String srcImageFile, String result,
|
int x, int y, int width, int height) {
|
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
|
byte[] byteArray=null;
|
try {
|
// 读取源图像
|
BufferedImage bi = ImageIO.read(new File(srcImageFile));
|
|
int srcWidth = bi.getWidth(); // 源图宽度
|
System.out.println("宽度为"+srcWidth);
|
int srcHeight = bi.getHeight(); // 源图高度
|
System.out.println("高度为"+srcHeight);
|
if (srcWidth > 0 && srcHeight > 0) {
|
Image image = bi.getScaledInstance(srcWidth, srcHeight,
|
Image.SCALE_DEFAULT);
|
// 四个参数分别为图像起点坐标和宽高
|
// 即: CropImageFilter(int x,int y,int width,int height)
|
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
|
Image img = Toolkit.getDefaultToolkit().createImage(
|
new FilteredImageSource(image.getSource(),
|
cropFilter));
|
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
Graphics g = tag.getGraphics();
|
g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
|
g.dispose();
|
// 输出为文件
|
File file= new File(result);
|
if(!file.exists()){
|
file.mkdirs();
|
}
|
ImageIO.write(tag, "JPEG",file);
|
//输出为byte[]数组
|
ImageIO.write(tag, "JPEG", outStream);
|
byteArray= outStream.toByteArray();
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return byteArray;
|
}
|
|
public static byte[] imageToByte(String path){
|
byte[] data = null;
|
FileImageInputStream input = null;
|
try {
|
input = new FileImageInputStream(new File(path));
|
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
byte[] buf = new byte[1024];
|
int numBytesRead = 0;
|
while ((numBytesRead = input.read(buf)) != -1) {
|
output.write(buf, 0, numBytesRead);
|
}
|
data = output.toByteArray();
|
output.close();
|
input.close();
|
}
|
catch (FileNotFoundException ex1) {
|
ex1.printStackTrace();
|
}
|
catch (IOException ex1) {
|
ex1.printStackTrace();
|
}
|
return data;
|
}
|
/**
|
* 图像切割(按指定起点坐标和宽高切割),切完之后上传图片服务器并返回路径
|
* @param fileInfo 源图像地址
|
* @param x 目标切片起点坐标X
|
* @param y 目标切片起点坐标Y
|
* @param width 目标切片宽度
|
* @param height 目标切片高度
|
*/
|
public FileInfos cutPhotoFromFast(FileInfos fileInfo,
|
int x, int y, int width, int height) {
|
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
|
byte[] byteArray=null;
|
FileInfos fileInfo1 = new FileInfos();
|
try {
|
// 读取源图像
|
String path = httpImgUrl+fileInfo.getPath();
|
log.info("源图像路径为"+path);
|
URL url = new URL(path);
|
BufferedImage bi = ImageIO.read(url);
|
|
int srcWidth = bi.getWidth(); // 源图宽度
|
System.out.println("宽度为"+srcWidth);
|
int srcHeight = bi.getHeight(); // 源图高度
|
System.out.println("高度为"+srcHeight);
|
if (srcWidth > 0 && srcHeight > 0) {
|
Image image = bi.getScaledInstance(srcWidth, srcHeight,
|
Image.SCALE_DEFAULT);
|
// 四个参数分别为图像起点坐标和宽高
|
// 即: CropImageFilter(int x,int y,int width,int height)
|
ImageFilter cropFilter = new CropImageFilter(x, y, width, height);
|
Image img = Toolkit.getDefaultToolkit().createImage(
|
new FilteredImageSource(image.getSource(),
|
cropFilter));
|
BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
Graphics g = tag.getGraphics();
|
g.drawImage(img, 0, 0, width, height, null); // 绘制切割后的图
|
g.dispose();
|
// // 输出为文件
|
// File file= new File(result);
|
// if(!file.exists()){
|
// file.mkdirs();
|
// }
|
// ImageIO.write(tag, "JPEG",file);
|
//输出为byte[]数组
|
ImageIO.write(tag, "JPEG", outStream);
|
byteArray= outStream.toByteArray();
|
// fileInfo1 = fastDFSUtil.uploadByByte(byteArray, fileInfo.getExtName());
|
FileInfos fileInfos = fastDFSUtil.uploadByByte(byteArray, fileInfo.getExtName());
|
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
return fileInfo1;
|
}
|
}
|