今天我们来手写一个对图片进行处理的工具类!!!
此工具类可以加载本地路径、URL路径下的图片,也可以对图片进行裁剪和去色处理哦!!!
有了这个工具类,读者以后处理图片就会更加随心自如了!!!
代码如下:
package com.yueqian.utils;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
public class ImageUtil {
/**
* 根据路径加载图片
* @param path
* @return
*/
public static Image getImageByLocalFilePath(String path){
//获取图片
Image result = null;
FileInputStream fis = null;
//获取输入流
try {
fis = new FileInputStream(path);
//读取输入路径
try {
result = ImageIO.read(fis);
} catch (IOException e) {
e.printStackTrace();
return null;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}finally {
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return result;
}
/**
* 根据url加载图片
* @param urlPath
* @return
*/
public static Image getImageByURLPath(String urlPath) {
//获取图片
Image result = null;
URL url = null;
try {
url = new URL(urlPath);
result = ImageIO.read(url);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
return result;
}
/**
* 设定图片的大小
* @return
*/
public static Image getScalImage(Image img,int width,int height) {
//过滤非法
if(img == null || width == 0 || height == 0 ) {
System.out.println("参数不正确!");
return null;
}
//获取图片的长和宽
int imgWidth = img.getWidth(null);
int imgheight = img.getHeight(null);
//记录真实图片的比例
double imgk = imgWidth * 1.0 / imgheight;
//记录自己准备的比例
double cusk = width * 1.0 / height;
//要缩放的比例
double scale = 1.0;
//存放图片要绘制的大小
int imgRestltWidth = 0;
int imgResultHeight = 0;
//计算图片存放的坐标
int sx1 = 0;
int sx2 = 0;
int sy1 = 0;
int sy2 = 0;
if(imgk > cusk) {
//以图片的高为准
imgResultHeight = imgheight;
imgRestltWidth = (int)(cusk * imgheight);
//计算图片绘制坐标
sx1 = (imgWidth - imgRestltWidth)/2;
sy1 = 0;
sx2 = sx1 + imgRestltWidth;
sy2 = imgheight;
}else {
//以图片的宽为准
imgRestltWidth = imgWidth;
imgResultHeight = (int)(imgResultHeight/cusk);
//计算图形的坐标
sx1 = 0;
sy1 = (imgheight - imgResultHeight)/2;
sx2 = imgWidth;
sy2 = sy1 + imgResultHeight;
}
//将图片绘制到图片缓冲器中
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取图片缓冲器的画笔
Graphics g = bi.getGraphics();
//使用画笔将图片画到缓冲器中
g.drawImage(img, 0, 0, width, height, sx1, sy1, sx2, sy2, null);
g.dispose();
return bi;
}
/**
* 得到图片的字节数组
* @param img
* @param formatName
* @return
*/
public static byte[] getImageBytes(Image img,String formatName) {
//过滤非法
if(img == null) {
System.out.println("图片为空!");
return null;
}
byte[] result = null;
//获得字节数组的输出流
ByteArrayOutputStream bao = new ByteArrayOutputStream();
//定义图片缓冲流
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
//得到画笔
Graphics g = bi.getGraphics();
//使用画笔画入图片缓冲流中
g.drawImage(img, 0, 0, null);
g.dispose();
try {
ImageIO.write((RenderedImage)bi, formatName, bao);
//将缓冲流中的数据转换为字节数组
result = bao.toByteArray();
} catch (IOException e) {
e.printStackTrace();
System.out.println("ERROR:IOException"+e.getMessage());
}
return result;
}
/**
* 根据图片的byte[]得到image图片
*
*/
public static Image getImageByBytes(byte[] bytes) {
//非法操作
if(bytes == null) {
System.out.println("bytes is null!");
return null;
}
//定义返回的图片
Image result = null;
//定义字节数组输入流
ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
try {
result = ImageIO.read(bai);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 去色
*/
public static Image grayImage(Image img) {
BufferedImage image = toBufferedImage(img);
int width = image.getWidth();
int height = image.getHeight();
BufferedImage grayImage = new BufferedImage(width, height, image.getType());
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int color = image.getRGB(i, j);
final int r = (color >> 16) & 0xff;
final int g = (color >> 8) & 0xff;
final int b = color & 0xff;
int gray = getAvg(r, g, b);// 均值法灰度化
// System.out.println("像素坐标:" + " x=" + i + " y=" + j + " 灰度值="
// + gray);
grayImage.setRGB(i, j, colorToRGB(0, gray, gray, gray));
}
}
ByteArrayOutputStream out = null;
ByteArrayInputStream byteIn = null;
byte[] imgBytes = null;
try {
out = new ByteArrayOutputStream();
ImageIO.write(grayImage, "JPEG", out);
byteIn = new ByteArrayInputStream(out.toByteArray());
// 根据图像的大小定义结果数组的长度
imgBytes = new byte[byteIn.available()];
// 使用 将tag图像缓冲区封装的输入流,读取图像到字节数组
byteIn.read(imgBytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (byteIn != null) {
try {
byteIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return getImageByBytes(imgBytes);
}
// 均值法
private static int getAvg(int x, int y, int z) {
int avg = (x + y + z) / 3;
return avg;
}
private static int colorToRGB(int alpha, int red, int green, int blue) {
int newPixel = 0;
newPixel += alpha;
newPixel = newPixel << 8;
newPixel += red;
newPixel = newPixel << 8;
newPixel += green;
newPixel = newPixel << 8;
newPixel += blue;
return newPixel;
}
/**
* Image 转换为 BufferedImage
*/
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
int type = BufferedImage.TYPE_INT_RGB;
BufferedImage bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
Graphics g = bimage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return bimage;
}
}
今天要分享的就这么多了!!!期待下期的精彩呈现吧!