一个用于图片处理的工具类
程序员文章站
2022-04-08 14:57:24
...
其中包括图片与字节流的相互转换,获取图片类型,图片大小,图片长宽等:
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import org.apache.commons.codec.binary.Base64;
/**
* This utility class is used for HTTP access operation, like URL encoding.
*
* @author Leo.zhou
*
*/
public class CommonUtils {
/**
* Convert byte stream base on base64 format to file
*
* @param stream, byte stream base on base64
* @return file convert from byte stream
* @throws IOException
*/
public static File convertToFileFromBase64(String stream, String path) throws IOException {
byte[] bytes = stream.getBytes("ASCII");
byte[] decoded = Base64.decodeBase64(bytes);
File file = new File(path);
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
file.createNewFile();
OutputStream os = new FileOutputStream(file);
try {
ByteArrayInputStream buffer = new ByteArrayInputStream(decoded);
byte[] barr = new byte[1024];
while(true) {
int r = buffer.read(barr);
if(r <= 0) {
break;
}
os.write(barr, 0, r);
}
} finally {
os.close();
}
return file;
}
/**
* Convert file to byte stream base on base64 format
*
* @param file file in file system
* @return byte stream convert from file
* @throws IOException
*/
public static String convertToBase64FromFile(File file) throws IOException {
InputStream in = new FileInputStream(file);
byte[] bytes = null;
try {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] barr = new byte[1024];
while (true) {
int r = in.read(barr);
if (r <= 0) {
break;
}
buffer.write(barr, 0, r);
}
bytes = buffer.toByteArray();
} finally {
in.close();
}
//All chars in encoded are guaranteed to be 7-bit ASCII
byte[] encoded = Base64.encodeBase64(bytes);
return new String(encoded, "ASCII");
}
/**
* Get image file format type
*
* @param imageFile image file
* @return image type
* @throws IOException
*/
public static String getImageFormatName(File imageFile) throws IOException {
ImageInputStream iis = null;
try {
iis= ImageIO.createImageInputStream(imageFile);
// Find all image readers that recognize the image format
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
if(iter == null || !iter.hasNext()) {
throw new IOException("image format error");
}
ImageReader reader = iter.next();
// Return the format name
return reader.getFormatName().toUpperCase();
} finally {
if(iis != null) {
iis.close();
}
}
}
/**
* Get image dimension
*
* @param imageFile image file
* @return image dimension
* @throws IOException
*/
public static String getImageDimension(File imageFile) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(imageFile);
BufferedImage sourceImg = ImageIO.read(fis);
if(sourceImg == null) {
throw new IOException("image format error");
}
String dimension = sourceImg.getWidth() + "*" + sourceImg.getHeight();
return dimension;
} finally {
if(fis != null) {
fis.close();
}
}
}
/**
* Get file size
*
* @param File file
* @return file size
* @throws IOException
*/
public static Integer getFileSize(File file) throws IOException {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return fis.available();
} finally {
if(fis != null) {
fis.close();
}
}
}
}