zoukankan      html  css  js  c++  java
  • png转tif

    发国外的文章要求图片是tif,cmyk色彩空间的。

    大小尺寸还有要求。

    比如

     

    网上大神多,找到了一段代码,感谢!

    https://www.jianshu.com/p/ec2af4311f56

    https://github.com/KevinZc007/image2Tif

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;

    import javax.imageio.ImageIO;
    import javax.imageio.stream.ImageOutputStream;
    import javax.media.jai.JAI;
    import javax.media.jai.PlanarImage;

    //import com.sun.media.imageio.plugins.tiff.TIFFField;
    import com.sun.media.imageio.plugins.tiff.TIFFTag;
    import com.sun.media.jai.codec.FileSeekableStream;
    import com.sun.media.jai.codec.TIFFEncodeParam;
    import com.sun.media.jai.codecimpl.TIFFImageEncoder;
    import com.sun.media.jai.codec.TIFFField;

    public class Png2TifConvert {
    /**
    *
    * 功能描述: 图片转tif格式
    *
    * @param: [fileAbsolutePath]
    * @return: java.lang.String
    * @auther: KevinZc  <- 就是他
    * @date: 2018/9/8 22:14
    */
    public static String image2Tif(String fileAbsolutePath){
    OutputStream outputStream = null;
    String filterFilePath = null;
    String tifFilePath = null;
    ImageOutputStream ios = null;
    try {
    // 解决位深度太小 start ====注意:8位深度的图片会出现文件损坏问题
    File picture = new File(fileAbsolutePath);
    BufferedImage img = ImageIO.read(picture);
    int colorSpaceType = img.getColorModel().getColorSpace().getType();
    System.out.print(colorSpaceType);
    // 统一进行一次过滤 转换成24位深度
    filterFilePath = fileAbsolutePath.substring(0, fileAbsolutePath.lastIndexOf("."))+".png";
    tifFilePath = filterFilePath.substring(0, filterFilePath.lastIndexOf("."))+".tif";
    ios = ImageIO.createImageOutputStream(new File(filterFilePath));
    ImageIO.write(ImageIO.read(picture),"png", ios);
    // 解决位深度太小 end
    FileSeekableStream stream = new FileSeekableStream(filterFilePath);
    PlanarImage in = JAI.create("stream", stream);
    OutputStream os = null;
    os = new FileOutputStream(tifFilePath);
    // 设置dpi为300
    TIFFEncodeParam param = new TIFFEncodeParam();
    param.setCompression(TIFFEncodeParam.COMPRESSION_NONE);
    TIFFField[] extras = new TIFFField[2];
    extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
    // extras[0] = new TIFFField(282, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) dpi, 1}, {0, 0}});
    extras[1] = new TIFFField(283, TIFFTag.TIFF_RATIONAL, 1, (Object) new long[][]{{(long) 300, 1}, {0, 0}});
    param.setExtraFields(extras);

    TIFFImageEncoder enc = new TIFFImageEncoder(os, param);
    try {
    enc.encode(in);
    os.flush();
    os.close();
    stream.close();
    } catch (Exception e) {
    // logger.error("{}",e );
    throw new RuntimeException(e);
    }
    return tifFilePath;
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    if (outputStream != null) {
    outputStream.close();
    }
    if (ios != null) {
    ios.close();
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return null;
    }

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    image2Tif("1.png");
    System.out.print("done");
    }

    }

    cmyk色彩空间,拿ps转比较好,不过没有ps,黑白图是否会影响?不清楚。。

    不过java可以实现RGB转CMYK色彩空间。首先看下图片是否是CMYK空间,通常不是,输出是6 ,CMYK是9

    BufferedImage img = ImageIO.read(picture);
    int colorSpaceType = img.getColorModel().getColorSpace().getType();
    System.out.print(colorSpaceType);

    cmyk和rgb应该也存在一定的转换关系,但是转换完了是否还能在电脑里显示?因为电脑是rgb的色彩空间吧、

    参考另外一个大神的方法,下面是转载的link,谢谢分享!

    https://blog.csdn.net/ybn187/article/details/52185269

    public static String readImage(String filename) throws IOException {
    File file = new File(filename);
    ImageInputStream input = ImageIO.createImageInputStream(file);
    Iterator readers = ImageIO.getImageReaders(input);
    if(readers == null || !readers.hasNext()) {
    throw new RuntimeException("1 No ImageReaders found");
    }
    ImageReader reader = (ImageReader) readers.next();
    reader.setInput(input);
    String format = reader.getFormatName() ;
    BufferedImage image;

    if ( "JPEG".equalsIgnoreCase(format) ||"JPG".equalsIgnoreCase(format) ) {
    try {
    // 尝试读取图片 (包括颜色的转换).
    image = reader.read(0); //RGB
    } catch (IIOException e) {
    // 读取Raster (没有颜色的转换).
    Raster raster = reader.readRaster(0, null);//CMYK
    image = createJPEG4(raster);
    }
    image.getGraphics().drawImage(image, 0, 0, null);
    String dstfilename = filename.substring(0,filename.lastIndexOf("."))+"_rgb"+filename.substring(filename.lastIndexOf("."));
    String newfilename = filename;
    File newFile = new File(dstfilename);
    FileOutputStream out = new FileOutputStream(newFile);
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    encoder.encode(image);
    out.flush();
    out.close();
    return dstfilename;
    }
    return null;
    }

    private static BufferedImage createJPEG4(Raster raster) {
    int w = raster.getWidth();
    int h = raster.getHeight();
    byte[] rgb = new byte[w * h * 3];
    //彩色空间转换
    float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
    float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
    float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
    float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
    for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
    float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i],
    cr = 255 - Cr[i];

    double val = y + 1.402 * (cr - 128) - k;
    val = (val - 128) * .65f + 128;
    rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
    : (byte) (val + 0.5);

    val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
    val = (val - 128) * .65f + 128;
    rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
    : (byte) (val + 0.5);

    val = y + 1.772 * (cb - 128) - k;
    val = (val - 128) * .65f + 128;
    rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff
    : (byte) (val + 0.5);
    }
    raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[]{0, 1, 2}, null);
    ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    return new BufferedImage(cm, (WritableRaster) raster, true, null);
    }

    public static String TestImg(String src) {
    File imgsrc = new File(src);
    try {
    ImageIO.read(imgsrc);
    } catch (IOException e) {
    // TODO Auto-generated catch block

    String msg = e.getMessage();
    System.out.println("msg:"+msg);
    if (msg.indexOf("Unsupported Image Type") == 0) {
    try {
    return readImage(src);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    } else {
    e.printStackTrace();
    return null;
    }
    }
    return src;

    }

  • 相关阅读:
    重新进入学习模式
    第八章 函数
    第七章 用户输入和while语句
    第6章 字典
    第五章 if语句
    对前四章方法、函数、语句的总结
    完成四个章节的学习,我觉得有必要花一天的时间对各章节内的函数、方法、语句进行总结。明后天不再对新章节进行学习。
    第四章 操作列表
    第三章 列表简介
    C语言数据结构-折半查找
  • 原文地址:https://www.cnblogs.com/marszhw/p/10388721.html
Copyright © 2011-2022 走看看