http://www.open-open.com/lib/view/open1394859355853.html
package com.pinker.util;
import java.awt.color;
import java.awt.graphics;
import java.awt.image;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.arrays;
import java.util.iterator;
import javax.imageio.iioimage;
import javax.imageio.imageio;
import javax.imageio.imagereader;
import javax.imageio.imagewriter;
import javax.imageio.stream.imageinputstream;
import javax.imageio.stream.imageoutputstream;
/**
* 图像裁剪以及压缩处理工具类
*
* 主要针对动态的gif格式图片裁剪之后,只出现一帧动态效果的现象提供凯发天生赢家一触即发官网的解决方案
*
* 提供依赖三方包凯发天生赢家一触即发官网的解决方案(针对gif格式数据特征一一解析,进行编码解码操作)
* 提供基于jdk image i/o 的凯发天生赢家一触即发官网的解决方案(jdk探索失败)
*/
public class imageutil2 {
public enum image_format{
bmp("bmp"),
jpg("jpg"),
wbmp("wbmp"),
jpeg("jpeg"),
png("png"),
gif("gif");
private string value;
image_format(string value){
this.value = value;
}
public string getvalue() {
return value;
}
public void setvalue(string value) {
this.value = value;
}
}
/**
* 获取图片格式
* @param file 图片文件
* @return 图片格式
*/
public static string getimageformatname(file file)throws ioexception{
string formatname = null;
imageinputstream iis = imageio.createimageinputstream(file);
iterator imagereader = imageio.getimagereaders(iis);
if(imagereader.hasnext()){
imagereader reader = imagereader.next();
formatname = reader.getformatname();
}
return formatname;
}
/************************* 基于三方包凯发天生赢家一触即发官网的解决方案 *****************************/
/**
* 剪切图片
*
* @param source 待剪切图片路径
* @param targetpath 裁剪后保存路径(默认为源路径)
* @param x 起始横坐标
* @param y 起始纵坐标
* @param width 剪切宽度
* @param height 剪切高度
*
* @returns 裁剪后保存路径(图片后缀根据图片本身类型生成)
* @throws ioexception
*/
public static string cutimage(string sourcepath , string targetpath , int x , int y , int width , int height) throws ioexception{
file file = new file(sourcepath);
if(!file.exists()) {
throw new ioexception("not found the image:" sourcepath);
}
if(null == targetpath || targetpath.isempty()) targetpath = sourcepath;
string formatname = getimageformatname(file);
if(null == formatname) return targetpath;
formatname = formatname.tolowercase();
// 防止图片后缀与图片本身类型不一致的情况
string pathprefix = getpathwithoutsuffix(targetpath);
targetpath = pathprefix formatname;
// gif需要特殊处理
if(image_format.gif.getvalue() == formatname){
gifdecoder decoder = new gifdecoder();
int status = decoder.read(sourcepath);
if (status != gifdecoder.status_ok) {
throw new ioexception("read image " sourcepath " error!");
}
animatedgifencoder encoder = new animatedgifencoder();
encoder.start(targetpath);
encoder.setrepeat(decoder.getloopcount());
for (int i = 0; i < decoder.getframecount(); i ) {
encoder.setdelay(decoder.getdelay(i));
bufferedimage childimage = decoder.getframe(i);
bufferedimage image = childimage.getsubimage(x, y, width, height);
encoder.addframe(image);
}
encoder.finish();
}else{
bufferedimage image = imageio.read(file);
image = image.getsubimage(x, y, width, height);
imageio.write(image, formatname, new file(targetpath));
}
//普通图片
bufferedimage image = imageio.read(file);
image = image.getsubimage(x, y, width, height);
imageio.write(image, formatname, new file(targetpath));
return targetpath;
}
/**
* 压缩图片
* @param sourcepath 待压缩的图片路径
* @param targetpath 压缩后图片路径(默认为初始路径)
* @param width 压缩宽度
* @param height 压缩高度
*
* @returns 裁剪后保存路径(图片后缀根据图片本身类型生成)
* @throws ioexception
*/
public static string zoom(string sourcepath , string targetpath, int width , int height) throws ioexception{
file file = new file(sourcepath);
if(!file.exists()) {
throw new ioexception("not found the image :" sourcepath);
}
if(null == targetpath || targetpath.isempty()) targetpath = sourcepath;
string formatname = getimageformatname(file);
if(null == formatname) return targetpath;
formatname = formatname.tolowercase();
// 防止图片后缀与图片本身类型不一致的情况
string pathprefix = getpathwithoutsuffix(targetpath);
targetpath = pathprefix formatname;
// gif需要特殊处理
if(image_format.gif.getvalue() == formatname){
gifdecoder decoder = new gifdecoder();
int status = decoder.read(sourcepath);
if (status != gifdecoder.status_ok) {
throw new ioexception("read image " sourcepath " error!");
}
animatedgifencoder encoder = new animatedgifencoder();
encoder.start(targetpath);
encoder.setrepeat(decoder.getloopcount());
for (int i = 0; i < decoder.getframecount(); i ) {
encoder.setdelay(decoder.getdelay(i));
bufferedimage image = zoom(decoder.getframe(i), width , height);
encoder.addframe(image);
}
encoder.finish();
}else{
bufferedimage image = imageio.read(file);
bufferedimage zoomimage = zoom(image , width , height);
imageio.write(zoomimage, formatname, new file(targetpath));
}
bufferedimage image = imageio.read(file);
bufferedimage zoomimage = zoom(image , width , height);
imageio.write(zoomimage, formatname, new file(targetpath));
return targetpath;
}
/*********************** 基于jdk 凯发天生赢家一触即发官网的解决方案 ********************************/
/**
* 读取图片
* @param file 图片文件
* @return 图片数据
* @throws ioexception
*/
public static bufferedimage[] readerimage(file file) throws ioexception{
bufferedimage sourceimage = imageio.read(file);
bufferedimage[] images = null;
imageinputstream iis = imageio.createimageinputstream(file);
iterator imagereaders = imageio.getimagereaders(iis);
if(imagereaders.hasnext()){
imagereader reader = imagereaders.next();
reader.setinput(iis);
int imagenumber = reader.getnumimages(true);
images = new bufferedimage[imagenumber];
for (int i = 0; i < imagenumber; i ) {
bufferedimage image = reader.read(i);
if(sourceimage.getwidth() > image.getwidth() || sourceimage.getheight() > image.getheight()){
image = zoom(image, sourceimage.getwidth(), sourceimage.getheight());
}
images[i] = image;
}
reader.dispose();
iis.close();
}
return images;
}
/**
* 根据要求处理图片
*
* @param images 图片数组
* @param x 横向起始位置
* @param y 纵向起始位置
* @param width 宽度
* @param height 宽度
* @return 处理后的图片数组
* @throws exception
*/
public static bufferedimage[] processimage(bufferedimage[] images , int x , int y , int width , int height) throws exception{
if(null == images){
return images;
}
bufferedimage[] oldimages = images;
images = new bufferedimage[images.length];
for (int i = 0; i < oldimages.length; i ) {
bufferedimage image = oldimages[i];
images[i] = image.getsubimage(x, y, width, height);
}
return images;
}
/**
* 写入处理后的图片到file
*
* 图片后缀根据图片格式生成
*
* @param images 处理后的图片数据
* @param formatname 图片格式
* @param file 写入文件对象
* @throws exception
*/
public static void writerimage(bufferedimage[] images , string formatname , file file) throws exception{
iterator imagewriters = imageio.getimagewritersbyformatname(formatname);
if(imagewriters.hasnext()){
imagewriter writer = imagewriters.next();
string filename = file.getname();
int index = filename.lastindexof(".");
if(index > 0){
filename = filename.substring(0, index 1) formatname;
}
string pathprefix = getfileprefixpath(file.getpath());
file outfile = new file(pathprefix filename);
imageoutputstream ios = imageio.createimageoutputstream(outfile);
writer.setoutput(ios);
if(writer.canwritesequence()){
writer.preparewritesequence(null);
for (int i = 0; i < images.length; i ) {
bufferedimage childimage = images[i];
iioimage image = new iioimage(childimage, null , null);
writer.writetosequence(image, null);
}
writer.endwritesequence();
}else{
for (int i = 0; i < images.length; i ) {
writer.write(images[i]);
}
}
writer.dispose();
ios.close();
}
}
/**
* 剪切格式图片
*
* 基于jdk image i/o凯发天生赢家一触即发官网的解决方案
*
* @param sourcefile 待剪切图片文件对象
* @param destfile 裁剪后保存文件对象
* @param x 剪切横向起始位置
* @param y 剪切纵向起始位置
* @param width 剪切宽度
* @param height 剪切宽度
* @throws exception
*/
public static void cutimage(file sourcefile , file destfile, int x , int y , int width , int height) throws exception{
// 读取图片信息
bufferedimage[] images = readerimage(sourcefile);
// 处理图片
images = processimage(images, x, y, width, height);
// 获取文件后缀
string formatname = getimageformatname(sourcefile);
destfile = new file(getpathwithoutsuffix(destfile.getpath()) formatname);
// 写入处理后的图片到文件
writerimage(images, formatname , destfile);
}
/**
* 获取系统支持的图片格式
*/
public static void getossupportsstandardimageformat(){
string[] readerformatname = imageio.getreaderformatnames();
string[] readersuffixname = imageio.getreaderfilesuffixes();
string[] readermimetype = imageio.getreadermimetypes();
system.out.println("========================= os supports reader ========================");
system.out.println("os supports reader format name : " arrays.aslist(readerformatname));
system.out.println("os supports reader suffix name : " arrays.aslist(readersuffixname));
system.out.println("os supports reader mime type : " arrays.aslist(readermimetype));
string[] writerformatname = imageio.getwriterformatnames();
string[] writersuffixname = imageio.getwriterfilesuffixes();
string[] writermimetype = imageio.getwritermimetypes();
system.out.println("========================= os supports writer ========================");
system.out.println("os supports writer format name : " arrays.aslist(writerformatname));
system.out.println("os supports writer suffix name : " arrays.aslist(writersuffixname));
system.out.println("os supports writer mime type : " arrays.aslist(writermimetype));
}
/**
* 压缩图片
* @param sourceimage 待压缩图片
* @param width 压缩图片高度
* @param heigt 压缩图片宽度
*/
private static bufferedimage zoom(bufferedimage sourceimage , int width , int height){
bufferedimage zoomimage = new bufferedimage(width, height, sourceimage.gettype());
image image = sourceimage.getscaledinstance(width, height, image.scale_smooth);
graphics gc = zoomimage.getgraphics();
gc.setcolor(color.white);
gc.drawimage( image , 0, 0, null);
return zoomimage;
}
/**
* 获取某个文件的前缀路径
*
* 不包含文件名的路径
*
* @param file 当前文件对象
* @return
* @throws ioexception
*/
public static string getfileprefixpath(file file) throws ioexception{
string path = null;
if(!file.exists()) {
throw new ioexception("not found the file !" );
}
string filename = file.getname();
path = file.getpath().replace(filename, "");
return path;
}
/**
* 获取某个文件的前缀路径
*
* 不包含文件名的路径
*
* @param path 当前文件路径
* @return 不包含文件名的路径
* @throws exception
*/
public static string getfileprefixpath(string path) throws exception{
if(null == path || path.isempty()) throw new exception("文件路径为空!");
int index = path.lastindexof(file.separator);
if(index > 0){
path = path.substring(0, index 1);
}
return path;
}
/**
* 获取不包含后缀的文件路径
*
* @param src
* @return
*/
public static string getpathwithoutsuffix(string src){
string path = src;
int index = path.lastindexof(".");
if(index > 0){
path = path.substring(0, index 1);
}
return path;
}
/**
* 获取文件名
* @param filepath 文件路径
* @return 文件名
* @throws ioexception
*/
public static string getfilename(string filepath) throws ioexception{
file file = new file(filepath);
if(!file.exists()) {
throw new ioexception("not found the file !" );
}
return file.getname();
}
/**
* @param args
* @throws exception
*/
public static void main(string[] args) throws exception {
// 获取系统支持的图片格式
//imagecutterutil.getossupportsstandardimageformat();
try {
// 起始坐标,剪切大小
int x = 100;
int y = 75;
int width = 100;
int height = 100;
// 参考图像大小
int clientwidth = 300;
int clientheight = 250;
file file = new file("c:\\1.jpg");
bufferedimage image = imageio.read(file);
double destwidth = image.getwidth();
double destheight = image.getheight();
if(destwidth < width || destheight < height)
throw new exception("源图大小小于截取图片大小!");
double widthratio = destwidth / clientwidth;
double heightratio = destheight / clientheight;
x = double.valueof(x * widthratio).intvalue();
y = double.valueof(y * heightratio).intvalue();
width = double.valueof(width * widthratio).intvalue();
height = double.valueof(height * heightratio).intvalue();
system.out.println("裁剪大小 x:" x ",y:" y ",width:" width ",height:" height);
/************************ 基于三方包凯发天生赢家一触即发官网的解决方案 *************************/
string formatname = getimageformatname(file);
string pathsuffix = "." formatname;
string pathprefix = getfileprefixpath(file);
string targetpath = pathprefix system.currenttimemillis() pathsuffix;
targetpath = imageutil2.cutimage(file.getpath(), targetpath, x , y , width, height);
string bigtargetpath = pathprefix system.currenttimemillis() pathsuffix;
imageutil2.zoom(targetpath, bigtargetpath, 100, 100);
string smalltargetpath = pathprefix system.currenttimemillis() pathsuffix;
imageutil2.zoom(targetpath, smalltargetpath, 50, 50);
/************************ 基于jdk image i/o 凯发天生赢家一触即发官网的解决方案(jdk探索失败) *************************/
// file destfile = new file(targetpath);
// imagecutterutil.cutimage(file, destfile, x, y, width, height);
} catch (ioexception e) {
e.printstacktrace();
}
}
}
posted on 2016-05-23 14:40
simone 阅读(1464)
评论(0) 编辑 收藏 所属分类:
java