将 tiff 转换 jpg 格式方法和所需jar包

所需jar包:
链接:https://pan.baidu.com/s/1GBBWEiBCHy5y3TbKhkTOfg
提取码:mfvs

/**
 * @描述 将 tiff 转换 jpg 格式
 * @参数 filePath
 * @返回值 turnJpgFile
 * @author by
 * @创建时间 2020/6/22 9:49
 */
public static String tiffTuanJPG(String filePath) {
    
    
    String format = filePath.substring(filePath.lastIndexOf(".") + 1);
    String turnJpgFile = filePath.replace("tif", "jpg");
    if (format.equals("tif")) {
    
    
        File fileTif = new File(turnJpgFile);
        if (fileTif.exists()) {
    
    
            System.out.println("该tif文件已经转换为 JPG 文件:" + turnJpgFile);
            return turnJpgFile;
        }
        //读取iff文件
        RenderedOp rd = JAI.create("fileload", filePath);
        OutputStream ops = null;
        try {
    
    
            ops = new FileOutputStream(turnJpgFile);
            //文件存储输出流
            JPEGEncodeParam param = new JPEGEncodeParam();
            //指定输出格式
            ImageEncoder image = ImageCodec.createImageEncoder("JPEG", ops, param);
            image.encode(rd);
            //解析输出流进行输出
            ops.close();
            System.out.println("tif转换jpg成功:" + filePath);
        } catch (FileNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    return turnJpgFile;
}
    @Test
    public void test() {
    
    
        String fileName = "D:\\home\\oldZJYX\\7a400398f2db482baec96e4f4d9e68a9\\扬州中行8269-201904.tif";
        String s = tiffTuanJPG(fileName);
        System.out.println(s);
    }

猜你喜欢

转载自blog.csdn.net/weixin_43614067/article/details/107528439
今日推荐