PNM格式图片转换与JAI ImageIO

 

问题:png转为pnm格式图片 

解决:代码如下

public static void main(String[] args) throws Exception {
        //tif/tiff/bmp/pbm转换png格式
        String pngFilePath = "/Users/test/111.jpg";
        String pbmFilePath = "/Users/test/111.pbm";
        test(pngFilePath,pbmFilePath);
    }


/**
     * //tif/tiff/bmp/pbm转换png格式
     * @param pngFilePath
     * @param pbmFilePath
     * @throws IOException
     */
public void test(String pngFilePath, String pbmFilePath) throws IOException {
        RenderedOp ro = JAI.create("fileload", pngFilePath);
        OutputStream os = new FileOutputStream(pbmFilePath);

        /*PNGEncodeParam param = new PNGEncodeParam.Gray();//no
        ImageEncoder ie = ImageCodec.createImageEncoder("PNG", os, param);*/

        /*BMPEncodeParam param = new BMPEncodeParam();//no
        ImageEncoder ie = ImageCodec.createImageEncoder("BMP", os,param);*/

        /*BMPEncodeParam param = new BMPEncodeParam();
        ImageEncoder ie = ImageCodec.createImageEncoder("BMP", os,param);*/

        PNMEncodeParam param = new PNMEncodeParam();//yes
//        param.setRaw(false);//true P6,false P3
        ImageEncoder ie = ImageCodec.createImageEncoder("PNM", os, param);
        ie.encode(ro);
//        ie.encode(in);
        os.flush();
        os.close();
        System.out.println("图片转换成功!");

    }

    public void test2(String pngFilePath, String pbmFilePath) throws IOException {
        FileSeekableStream stream = new FileSeekableStream(pngFilePath);
        PlanarImage in = JAI.create("stream", stream);
        OutputStream os = null;
        os = new FileOutputStream(pbmFilePath);
        PNMEncodeParam param = new PNMEncodeParam();//yes

        ImageEncoder enc = ImageCodec.createImageEncoder("PNM", os, param);
        try {
            enc.encode(in);
            os.flush();
            os.close();
            stream.close();
        } catch (IOException e) {
            System.out.println("error" + e.toString());
        }
    }

maven仓库 javax.media.jai


      
          javax.media.jai
          com.springsource.javax.media.jai.core
          1.1.3
      

 

 

你可能感兴趣的:(PNM格式图片转换与JAI ImageIO)