Skip to content

Instantly share code, notes, and snippets.

@safeJar
Created May 28, 2018 03:47
Show Gist options
  • Select an option

  • Save safeJar/676f701ae20f1a60abfbe676a3e34574 to your computer and use it in GitHub Desktop.

Select an option

Save safeJar/676f701ae20f1a60abfbe676a3e34574 to your computer and use it in GitHub Desktop.
裁剪图片
package com.inno.utils;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Iterator;
/**
* Created by kenny on 2018/5/26.
*/
public class ImageUtil {
//图片是绝对路径
public static String getImageBaseStringByFilename(String filename){
InputStream in = null;
byte[] data = null;
//读取图片字节数组
try
{
in = new FileInputStream(filename);
data = new byte[in.available()];
in.read(data);
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
//对字节数组Base64编码
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);//返回Base64编码过的字节数组字符串
}
public static void main(String[] args) {
//System.out.println(getFileExt("C:\\Users\\kenny\\Desktop\\image\\4e7e1b81f9604d47a4dc3bd27595e036.jpg"));
try {
cut(114, 236, 329, 236,
"C:\\Users\\kenny\\Desktop\\image\\bd77a966bdf2419aba38c46bca30d2fe.jpg",
"C:\\Users\\kenny\\Desktop\\image\\cuts\\cut_bd77a966bdf2419aba38c46bca30d2fe.jpg");
} catch (IOException e) {
e.printStackTrace();
}
}
public static final String TYPE_JPG = "jpg";
public static final String TYPE_GIF = "gif";
public static final String TYPE_PNG = "png";
public static final String TYPE_BMP = "bmp";
public static final String TYPE_UNKNOWN = "unknown";
/**
* 根据文件流判断图片类型
* @param fis
* @return jpg/png/gif/bmp
*/
public static String getPicType(FileInputStream fis) {
//读取文件的前几个字节来判断图片格式
byte[] b = new byte[4];
try {
fis.read(b, 0, b.length);
String type = bytesToHexString(b).toUpperCase();
if (type.contains("FFD8FF")) {
return TYPE_JPG;
} else if (type.contains("89504E47")) {
return TYPE_PNG;
} else if (type.contains("47494638")) {
return TYPE_GIF;
} else if (type.contains("424D")) {
return TYPE_BMP;
}else{
return TYPE_UNKNOWN;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
/**
* byte数组转换成16进制字符串
* @param src
* @return
*/
public static String bytesToHexString(byte[] src){
StringBuilder stringBuilder = new StringBuilder();
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
}
public static void cut(int x,int y,int width,int height,String srcpath,String subpath) throws IOException {//裁剪方法
FileInputStream is = null;
ImageInputStream iis = null;
try {
is = new FileInputStream(srcpath); //读取原始图片
String suffix = getPicType(new FileInputStream(new File(srcpath)));
Iterator<ImageReader> it = ImageIO.getImageReadersByFormatName(suffix); //ImageReader声称能够解码指定格式
ImageReader reader = it.next();
iis = ImageIO.createImageInputStream(is); //获取图片流
reader.setInput(iis, true); //将iis标记为true(只向前搜索)意味着包含在输入源中的图像将只按顺序读取
ImageReadParam param = reader.getDefaultReadParam(); //指定如何在输入时从 Java Image I/O框架的上下文中的流转换一幅图像或一组图像
Rectangle rect = new Rectangle(x, y, width, height); //定义空间中的一个区域
param.setSourceRegion(rect); //提供一个 BufferedImage,将其用作解码像素数据的目标。
BufferedImage bi = reader.read(0, param); //读取索引imageIndex指定的对象
File destFile = new File(subpath);
ImageIO.write(bi, suffix, destFile); //保存新图片
Runtime.getRuntime().exec("chmod 664 "+subpath);
} finally {
if (is != null)
is.close();
if (iis != null)
iis.close();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment