java jimi_绝对经典Java开源工具Jimi处理图片大小及格式转换及打水印(源码)

刚转换完视频,现在又得做图片转换,用了java的开源软件jimi图片处理工具,编写了一个图片转换工具类,包括了给图片打水印,给大家分享。

import java.awt.Image;

import java.awt.image.BufferedImage;

import java.awt.image.ImageProducer;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

import com.sun.jimi.core.Jimi;

import com.sun.jimi.core.JimiException;

import com.sun.jimi.core.JimiWriter;

import com.sun.jimi.core.options.JPGOptions;

public class TransferPicture {

/**

* @param source

* @param dest

* @param quality

* 图片格式转换

*/

public void toJPG(String source, String type, int quality) {

//0

if (quality  100 || (quality + "") == null || (quality + "").equals("")) {

System.out.println("quality must between ’0’ and ’100’");

System.out.println("set to DEFAULT value:’75’");

quality = 75;

}

String outfile = ConvertUtil.getFilename(source)+type;

try {

JPGOptions options = new JPGOptions();

options.setQuality(quality);

ImageProducer image = Jimi.getImageProducer(source);

JimiWriter writer = Jimi.createJimiWriter(outfile);

writer.setSource(image);

// 加入属性设置,非必要

// /*

writer.setOptions(options);

// */

writer.putImage(outfile);

} catch (JimiException je) {

System.err.println("Error: " + je);

}

}

/**

* @param source

* @param dest

* @throws JimiException

*/

public void toGIF(String source, String dest) throws JimiException {

if (dest == null || dest.trim().equals(""))

dest = source;

// 1:转换为jpg

if (!dest.toLowerCase().trim().endsWith("jpg")) {

dest += ".jpg";

}

toJPG(source, dest, 75);

BufferedImage file_in = null;

File file = new File(dest);

try {

file_in = javax.imageio.ImageIO.read(file);

} catch (Exception e) {

e.printStackTrace();

}

int end = dest.lastIndexOf(".");

file.deleteOnExit();

// output *.gif

file.renameTo(new File(dest.substring(0, end) + ".gif"));

// jpg to gif

AnimatedGifEncoder e = new AnimatedGifEncoder();

e.start(dest);

e.addFrame(file_in);

e.finish();

/*

//分解GIF:

GifDecoder d = new GifDecoder();

d.read("sample.gif");

int n = d.getFrameCount();

for(int i = 0; i 

BufferedImage frame = d.getFrame(i); // frame i

int t = d.getDelay(i);  // display duration of frame in milliseconds

// do something with frame

}

//合成GIF:

AnimatedGifEncoder e = new AnimatedGifEncoder();

e.start(outputFileName);

e.setDelay(1000); // 1 frame per sec

e.addFrame(image1);

e.addFrame(image2);

e.finish();

*/

}

/**

* @param img

* @param dest

* @throws JimiException

*/

public void toTIF(Image img, String dest) throws JimiException {

if (!dest.toLowerCase().trim().endsWith("tif")) {

dest += ".tif";

System.out.println("Overriding to TIF, output file: " + dest);

}

dest = dest.substring(0, dest.lastIndexOf(".")) + ".jpg";

try{

System.out.println("toTIF encode");

JimiWriter writer = Jimi.createJimiWriter(dest);

writer.setSource(img);

dest = dest.substring(0, dest.lastIndexOf(".")) + ".tif";

writer.putImage(dest);

}catch(Exception e){e.printStackTrace();}

}

/**

* 线性改变图片尺寸(可同时改变图片格式)

* @param source

* 源文件完整路径

* @param desc

* 目标文件完整路径

* @param ins

* 放大/缩小比率

* @throws JimiException

* @throws IOException

*/

public void changeDimension(String source, String desc, double ins) throws JimiException, IOException {

String temp = desc;

File _file = null;

if (desc == null || desc.trim().equals(""))

desc = source;

if (!desc.toLowerCase().trim().endsWith("jpg")) {

temp = desc.trim() + ".jpg";

}

this.toJPG(source, temp, 75);

_file = new File(temp); // 读入文件

Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象

double wideth = (double) src.getWidth(null); // 得到源图宽

double height = (double) src.getHeight(null); // 得到源图长

System.out.println("源图宽:"+wideth);

System.out.println("源图长:"+height);

/*//缩放处理

int iWideth = (int) (wideth * ins);

int iHeight = (int) (height * ins);

*/

int iWideth = 300;

int iHeight = 200;

System.out.println("现图宽:"+iWideth);

System.out.println("现图长:"+iHeight);

System.out.println("缩放参数:"+ins);

BufferedImage tag = null;

try{

tag = new BufferedImage(iWideth, iHeight,BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图

/*//打水印

File _filebiao = new File("test2.jpg");

Image src_biao = ImageIO.read(_filebiao);

int wideth_biao = 40;//src_biao.getWidth(null);

int height_biao = 40;//src_biao.getHeight(null);

tag.getGraphics().drawImage(src_biao,50,50,wideth_biao,height_biao,null);

*/

}catch(Exception e){e.printStackTrace();}

if (!temp.trim().equals(desc))

_file.deleteOnExit();

if (desc.toLowerCase().trim().endsWith("gif")) {

System.out.println("the type is gif!");

AnimatedGifEncoder e = new AnimatedGifEncoder();

e.start(desc);

e.addFrame(tag);

e.finish();

} else if (desc.toLowerCase().trim().endsWith("tif") || desc.toLowerCase().trim().endsWith("tiff")) {

System.out.println("the type is tif!");

this.toTIF(tag, desc);

} else {

try{

System.out.println("common type!");

JimiWriter writer = Jimi.createJimiWriter(desc);

writer.setSource(tag);

writer.putImage(desc);

}catch(Exception ex){ex.printStackTrace();}

}

}

public void transpic(String infile,int iWideth, int iHeight, String type) throws JimiException, IOException {

String temp = infile;

File _file = null;

/*if (outfile == null || outfile.trim().equals(""))

outfile = infile;

if (!outfile.toLowerCase().trim().endsWith("jpg")) {

temp = outfile.trim() + ".jpg";

}*/

ConvertUtil cu = new ConvertUtil();

if(!cu.getFiletype(infile).equals("jpg")){

this.toJPG(infile, temp, 75);

}

_file = new File(temp); // 读入文件

Image src = javax.imageio.ImageIO.read(_file); // 构造Image对象

double wideth = (double) src.getWidth(null); // 得到源图宽

double height = (double) src.getHeight(null); // 得到源图长

System.out.println("源图宽:"+wideth);

System.out.println("源图长:"+height);

/*//缩放处理

int iWideth = (int) (wideth * ins);

int iHeight = (int) (height * ins);

*/

//int iWideth = 300;

//int iHeight = 200;

System.out.println("现图宽:"+iWideth);

System.out.println("现图长:"+iHeight);

String outfile = "";

BufferedImage tag = null;

try{

tag = new BufferedImage(iWideth, iHeight,BufferedImage.TYPE_INT_RGB);

tag.getGraphics().drawImage(src, 0, 0, iWideth, iHeight, null); // 绘制缩小后的图

/*//打水印

File _filebiao = new File("test2.jpg");

Image src_biao = ImageIO.read(_filebiao);

int wideth_biao = 40;//src_biao.getWidth(null);

int height_biao = 40;//src_biao.getHeight(null);

tag.getGraphics().drawImage(src_biao,50,50,wideth_biao,height_biao,null);

*/

}catch(Exception e){e.printStackTrace();}

if (!temp.trim().equals(outfile))

_file.deleteOnExit();

if (outfile.toLowerCase().trim().endsWith("gif")) {

System.out.println("the type is gif!");

AnimatedGifEncoder e = new AnimatedGifEncoder();

e.start(outfile);

e.addFrame(tag);

e.finish();

} else if (outfile.toLowerCase().trim().endsWith("tif") || outfile.toLowerCase().trim().endsWith("tiff")) {

System.out.println("the type is tif!");

this.toTIF(tag, outfile);

} else {

try{

System.out.println("common type!");

JimiWriter writer = Jimi.createJimiWriter(outfile);

writer.setSource(tag);

writer.putImage(outfile);

}catch(Exception ex){ex.printStackTrace();}

}

}

public static void main(String[] args) {

String dest = "test.jpg";

String source = "test.gif";

int quality = 100;

double ins = 4;

TransferPicture tp = new TransferPicture();

try{

System.out.println("before change");

System.out.println("-------------------");

//tp.toJPG(source,dest,quality);

//tp.toGIF(source, dest);

tp.changeDimension(source, dest, ins);

System.out.println("-------------------");

System.out.println("end change");

}catch(Exception e){

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值