java tif合成_JAI 多图片合成TIF格式
/***
*@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;
}
java tif合成_JAI 多图片合成TIF格式相关推荐
- java gif转jpg_Java gif图片转换为jpg格式
下面通过代码给大家介绍Java gif图片转换为jpg格式,具体代码如下所示: if(fileName.toLowerCase().endsWith(".gif")){//由于头像 ...
- java gif转jpg_Java gif图片转换为jpg格式|chu
下面通过代码给大家介绍Java gif图片转换为jpg格式,具体代码如下所示: if(fileName.toLowerCase().endsWith(".gif")){//由于头像 ...
- python 透明图片合成_python:图片合成(PIL 库Image类模块)
实现两张图的合成.两种情况:两张非透明图的合成:涉及透明图的合成. 1.第一类:直接将两张图和在一起就行,选好位置粘贴覆盖.(适合规则图合并) from PIL import Image #加载底图 ...
- php 图片合成,php原生图片合成和文字生成图片
之前有一个我写的帖子是可以拿到远程地址的图片保存本地的 前段时间搞的很火的朋友圈标签,可以使用下面仅仅三个自定义函数即可合成,具体参数石头已经给大家写出,有需要改进的地方请直接留言! /** * a. ...
- 使用canvas在前端实现图片合成
看着总结的不错,我也就拿来主义了,做个记录,侵权必删 图片合成最常见的需求有验证码图片,亦或者图片加水印等,这种实现一般都是后端实现的. 随着HTML5发展和现代浏览器的占比越来越高,我们其实也可以在 ...
- java合成tif图片_C# 将图片合成tif文件
[实例简介] [实例截图] [核心代码] using BitMiracle.LibTiff.Classic; using System.Drawing; using System.Drawing.Im ...
- Java 实现图片合成
2019独角兽企业重金招聘Python工程师标准>>> 图片合成 利用Java的绘图方法,实现图片合成 在开始之前,先定一个小目标,我们希望通过图片合成的方式,创建一个类似下面样式的 ...
- java 处理图片图片合成
最近写了一个java的图片合成相关的项目,真的是一踩一个坑,下面博主说说遇见的一些坑和怎么解决的这些问题. 本文主要讲图片合成,加文字还是比较简单的. 先讲下我的需求,把一张图片盖到另外一张图片上面, ...
- Java图片合成并引入外部字体写入文字
业务需要将两个图片合成,并按指定字体写入标语以及用户的经纬度,代码如下: import cn.hutool.core.img.GraphicsUtil;import javax.imageio.Ima ...
最新文章
- JAVA_day2_运算符
- 2016年研究数据可视化最不应该错过的10篇文章
- Spring Cloud Gateway 突发高危漏洞,下一代云原生网关恰逢其时?
- 上网本 ubuntu debian android,关于Debian:在Android上的chroot ubuntu 16.04上,apt-get更新失败...
- 怎么设置电脑屏幕一直亮着_电脑屏幕出现条纹是什么原因 花屏怎么解决?
- Spring框架学习笔记10:基于XML配置方式SSM框架西蒙购物网
- canvasnest 移动距离_GitHub - XiaoxinJiang/canvas-nest: 仿知乎登录页面canvas-nest
- 《Redis开发与运维》学习第五章
- 中国未来家庭STEAM教育趋势研究报告
- 2.2析取范式与合取范式
- 华为智慧屏V55升级鸿蒙2,华为智慧屏s55和v55对比参数_华为智慧屏s55和v55哪个好...
- 固定贴片电阻器封装尺寸
- 2007经典搞笑警句
- python画图常规设置
- 什么叫单模光纤_石家庄某小区光纤熔接示意图
- linux 函数中打印调用栈
- js数组对象按照中文拼音排序
- 2021年全球高级计量基础设施(AMI)收入大约3202.8百万美元,预计2028年达到4764.5百万美元
- SCC(四):Palette mode
- DGA - 研究内容整理