java tif合成_JAI 多图片合成TIF格式

该方法通过Java的Java Advanced Imaging (JAI)库将多个图像文件合并成一个TIFF文件。根据参数`isCompress`决定是否进行JPEG压缩,支持BMP, JPG, GIF和PNG格式的图片。转换过程会将非JPEG格式的图片转换为JPEG,以适应TIFF的合成。最后,方法返回生成的TIFF文件路径。" 113946778,10296596,BOM成本报表开发:MySQL存储过程详解,"['数据库管理', '报表开发', '金蝶K3', 'BOM表']
摘要由CSDN通过智能技术生成

/***

*@paramimageFileList 图片路径列表 (图片为E:/xx/xx.jpg格式)

*@paramtoPath tif文件所放路径

*@paramdistFileName tif文件名

*@paramconvertPath 转换图片存放路径

*@paramisCompress 是否压缩(压缩的时候是先将不是jpg的格式转为jpg,因为JAI可以设置JPEG的压缩模式。

* 但是png转jpg会有点失真,所以建议用非压缩的方式!!!)*/

public static String manyImgToTif(List imageFileList,String toPath, String distFileName, String convertPath, booleanisCompress) {

String tifFile= null;if(imageFileList != null && imageFileList.size()>0){

List fileArr = new ArrayList();

String fileName;

String imageFile;

String fileType= "";

File tmpFile ;

FileInputStream tmpIns= null;boolean isNeedTransfer = false;for (int i = 0; i < imageFileList.size(); i++){

imageFile=imageFileList.get(i);

String[] tempFile= imageFile.split("/");

fileName= tempFile[tempFile.length-1];

tmpFile= newFile(imageFile);//处理文件

try{//以防是png格式的图片直接改后缀的情况

tmpIns = newFileInputStream(tmpFile);

fileType=getPicType(tmpIns);if(isCompress){

File convertFile= newFile(convertPath);if (!convertFile.exists()) {

convertFile.mkdirs();

}if ("bmp".equals(fileType)|| "jpg".equals(fileType)){//有时图片有损坏,但是能展示,就是用这个架包的时候会报错,//比较严格,所以需要捕获异常做一下转换

isNeedTransfer = false;try{//需要重新获取流,因为上面判断格式已经读过流了.

tmpIns = newFileInputStream(tmpFile);

JPEGImageDecoder decoder=JPEGCodec.createJPEGDecoder(tmpIns);

decoder.decodeAsBufferedImage();

}catch(TruncatedFileException e){

e.printStackTrace();

logger.info("图片有损坏,需要做转换,image:"+imageFile);

isNeedTransfer= true;

}if(isNeedTransfer){

fileArr.add(pngtoJpg(imageFile,convertPath,fileName));

}else{

fileArr.add(newFile(imageFile));

}

}else if("gif".equals(fileType)){

fileArr.add(giftoJpg(imageFile,convertPath,fileName));

}else if("png".equals(fileType)){

fileArr.add(pngtoJpg(imageFile,convertPath,fileName));

}

}else{if ("bmp".equals(fileType)|| "jpg".equals(fileType)|| "gif".equals(fileType)|| "png".equals(fileType)){

fileArr.add(newFile(imageFile));

}

}

}catch(Exception e) {

e.printStackTrace();

}finally{if(tmpIns != null){try{

tmpIns.close();

}catch(IOException e) {

e.printStackTrace();

}

}

}

}if (fileArr.size() > 0) {try{

ArrayList pages= new ArrayList(fileArr.size() - 1);

FileSeekableStream[] stream= newFileSeekableStream[fileArr.size()];for (int i = 0; i < fileArr.size(); i++) {

stream[i]= newFileSeekableStream(fileArr.get(i).getCanonicalPath());

}

ParameterBlock pb= (newParameterBlock());

PlanarImage firstPage= JAI.create("stream", stream[0]);for (int i = 1; i < fileArr.size(); i++) {

PlanarImage page= JAI.create("stream", stream[i]);

pages.add(page);

}

TIFFEncodeParam param= newTIFFEncodeParam();if(isCompress){

param.setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);

}

TIFFField[] extras= new TIFFField[4];

extras[0] = new TIFFField(262, TIFFField.TIFF_SHORT, 1, (Object) new short[] { 6});

extras[1] = new TIFFField(282, 5, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});

extras[2] = new TIFFField(283, 5, 1, (Object) new long[][]{{(long) 200, 1}, {0, 0}});

extras[3] = new TIFFField(258, TIFFField.TIFF_SHORT, 1, (Object) new char[] { 8});

param.setExtraFields(extras);

param.setExtraImages(pages.iterator());

File f= newFile(toPath);if (!f.exists()) {

f.mkdirs();

}

tifFile= toPath + "\\"+ distFileName+".tif";

OutputStream os= newFileOutputStream(tifFile);

ImageEncoder enc= ImageCodec.createImageEncoder("tiff", os, param);

enc.encode(firstPage);//关掉流

os.flush();

os.close();for (int i = 0; i < fileArr.size(); i++) {

stream[i].close();

}

logger.info("====manyImgToTif done====");

}catch(IOException e) {

e.printStackTrace();

}

}

}returntifFile;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值