`
ErHuo
  • 浏览: 21015 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

WMF EMF SVG PNG Java图片处理

阅读更多
代码就示例贴点

工作需要转换难度大的就是EMF的转换了

使用的是freehep包

有需要的话可留言,联系我
EMF2SVG
public  class EmfToSVG extends EMFConverter {
	public static void main(String[] args) {
		String[] file = new String[2];
		file[0] = "D:\\input.emf";
		
		if (file == null || file.length == 0 || file[0] == null || file[0].length() == 0) {
            System.out.println("usage: EMF2SVG imput.emf [output.svg]");
            return;
        }
        export(ImageConstants.SVG, file[0], file.length > 1 ? file[1] : null);
	}
	
	protected static void export(String type, String srcFileName, String destFileName) {
        try {
            List exportFileTypes = ExportFileType.getExportFileTypes(type);
            if (exportFileTypes == null || exportFileTypes.size() == 0) {
                System.out.println(
                    type + " library is not available. check your classpath!");
                return;
            }

            ExportFileType exportFileType = (ExportFileType) exportFileTypes.get(0);

            // read the EMF file
            EMFRenderer emfRenderer = new EMFRenderer(
                new EMFInputStream(
                    new FileInputStream(srcFileName)));

            // create the destFileName,
            // replace or add the extension to the destFileName
            if (destFileName == null || destFileName.length() == 0) {
                // index of the beginning of the extension
                int lastPointIndex = srcFileName.lastIndexOf(".");

                // to be sure that the point separates an extension
                // and is not part of a directory name
                int lastSeparator1Index = srcFileName.lastIndexOf("/");
                int lastSeparator2Index = srcFileName.lastIndexOf("\\");

                if (lastSeparator1Index > lastPointIndex ||
                    lastSeparator2Index > lastPointIndex) {
                    destFileName = srcFileName + ".";
                } else if (lastPointIndex > -1) {
                    destFileName = srcFileName.substring(
                        0, lastPointIndex + 1);
                }

                // add the extension
                destFileName += type.toLowerCase();
            }

            // TODO there is no possibility to use Constants of base class!
            /* create SVG properties*/
            Properties p = new Properties();
            p.put(SVGGraphics2D.EMBED_FONTS, Boolean.toString(false));
            p.put(SVGGraphics2D.CLIP, Boolean.toString(true));
            p.put(SVGGraphics2D.COMPRESS, Boolean.toString(false));
            p.put(SVGGraphics2D.TEXT_AS_SHAPES, Boolean.toString(false));
            p.put(SVGGraphics2D.FOR, "Freehep EMF2SVG");
            p.put(SVGGraphics2D.TITLE, srcFileName);

            EMFPanel emfPanel = new EMFPanel();
            emfPanel.setRenderer(emfRenderer);

            // TODO why uses this classes components?!
            exportFileType.exportToFile(
               new File(destFileName),
               emfPanel,
               emfPanel,
               p,
               "Freehep EMF converter");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


WmfToSvg

/**
 * @author LiJie
 * 将wmf转换为svg格式的
 * 
 * 
 */
public class WmfToSvg {
    public void converter(byte[] wmfBytes, String dest) throws Exception {
         InputStream in = new ByteArrayInputStream(wmfBytes);
         WmfParser parser = new WmfParser();
         final SvgGdi gdi = new SvgGdi(false);
         parser.parse(in, gdi);
         Document doc = gdi.getDocument();
         OutputStream out = new FileOutputStream(dest);
         if (dest.endsWith(".svgz")) {
              out = new GZIPOutputStream(out);
         }
         output(doc, out,dest);
    }
    public void convert(String file,String dest) throws Exception{
         InputStream in = new FileInputStream(file);    
         WmfParser parser = new WmfParser();
         final SvgGdi gdi = new SvgGdi(false);
         parser.parse(in, gdi);
         Document doc = gdi.getDocument();
         OutputStream out = new FileOutputStream(dest);
         if (dest.endsWith(".svgz")) {
             out = new GZIPOutputStream(out);
         }
         output(doc, out,dest);
    }
    public byte[] encodeConvert() {
         return null;
    }

    private void output(Document doc, OutputStream out,String dest) throws Exception {
         TransformerFactory factory = TransformerFactory.newInstance();
         Transformer transformer = factory.newTransformer();
         transformer.setOutputProperty(OutputKeys.METHOD, "xml");
         transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");
         transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC,"-//W3C//DTD SVG 1.0//EN");
         transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");
         transformer.transform(new DOMSource(doc), new StreamResult(out));
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         transformer.transform(new DOMSource(doc), new StreamResult(bos));
         out.flush();
         out.close();
         //将svg直接转为jpg
         /*JPEGTranscoder it = new JPEGTranscoder();
         ByteArrayOutputStream jpg = new ByteArrayOutputStream();
         it.transcode(new TranscoderInput(new ByteArrayInputStream(bos.toByteArray())), new TranscoderOutput(jpg));
         String jpgFile=dest.replaceAll("svg","jpg");
         FileOutputStream jpgOut=new FileOutputStream(jpgFile);
         jpgOut.write(jpg.toByteArray());
         jpgOut.flush();*/
    }
}


SvgToImg
/**
 * @author LiJie
 * 将svg转换为jpg,png格式的
 * 
 * 
 */
public class SvgToImg {
    public void convert2JPEG(String file) throws TranscoderException, IOException {
        /*InputStream in = new FileInputStream(file);
        JPEGTranscoder transcoder = new JPEGTranscoder();
        transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
                 "org.apache.crimson.parser.XMLReaderImpl");
        transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,new Float(1.0));
        TranscoderInput input = new TranscoderInput(in);
        OutputStream ostream = new FileOutputStream(file.substring(0,file.lastIndexOf("."))+".jpg");
        TranscoderOutput output = new TranscoderOutput(ostream);
        transcoder.transcode(input, output);
        ostream.close();
        ostream.flush();*/       
        JPEGTranscoder t = new JPEGTranscoder();
        InputStream in = new FileInputStream(file);
        TranscoderInput input = new TranscoderInput(in);
        FileOutputStream outputStream = new FileOutputStream(new File(
                file.substring(0,file.lastIndexOf("."))+".jpg"));
        TranscoderOutput output = new TranscoderOutput(outputStream);
        t.transcode(input, output);
        outputStream.flush();
        outputStream.close();
        
    }
/*    public void convert2GIF(String file) throws TranscoderException, IOException{
        GIFTranscoder t = new GIFTranscoder();
        TranscoderInput input = new TranscoderInput(new FileInputStream(file));
        OutputStream ostream = new FileOutputStream(file.substring(0,file.lastIndexOf("."))+".gif");
        TranscoderOutput output = new TranscoderOutput(ostream);
        t.transcode(input, output);
        ostream.flush();
        ostream.close();
    }*/
    public void convert2PNG(String file) {
        try {
            PNGTranscoder t = new PNGTranscoder();
            InputStream in = new FileInputStream(file);
            TranscoderInput input = new TranscoderInput(in);
            FileOutputStream outputStream = new FileOutputStream(new File(
                    file.substring(0,file.lastIndexOf("."))+".png"));
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
            outputStream.close();

         } catch (Exception ex) {
             ex.printStackTrace();
         }
    }
    
    /**
     * 将svg字符串转换为png
     * 
     * @param svgCode svg代码
     * @param pngFilePath 保存的路径
     * @throws TranscoderException svg代码异常
     * @throws IOException io错误
     */
    public static void convertToPng(String svgCode, String pngFilePath) throws IOException,
            TranscoderException {

        File file = new File(pngFilePath);

        FileOutputStream outputStream = null;
        try {
            file.createNewFile();
            outputStream = new FileOutputStream(file);
            convertToPng(svgCode, outputStream);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 将svgCode转换成png文件,直接输出到流中
     * 
     * @param svgCode svg代码
     * @param outputStream 输出流
     * @throws TranscoderException 异常
     * @throws IOException io异常
     */
    public static void convertToPng(String svgCode, OutputStream outputStream)
            throws TranscoderException, IOException {
        try {
            byte[] bytes = svgCode.getBytes("utf-8");
            PNGTranscoder t = new PNGTranscoder();
            TranscoderInput input = new TranscoderInput(new ByteArrayInputStream(bytes));
            TranscoderOutput output = new TranscoderOutput(outputStream);
            t.transcode(input, output);
            outputStream.flush();
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    public static void main(String[] args) {
    	try {
			convertToPng("D:\\input.svg", "D:\\output.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TranscoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
    
分享到:
评论
1 楼 magic_fqz 2015-12-22  
你好,请问如何使用Java将EMF转JPG,有代码可以参考下吗,谢谢。

相关推荐

    JAVA WMF 转换SVG,PNG

    WMF转PNG EMF转PNG 工具类,使用wmf2svg 来实现wmf转svg 用batik jar实现svg转png 这种转换 不存在图片丢失等问题!

    wmf转svgz、png

    wmf转svgz、png

    any2pdforg.py

    提供图像批量转换程序,可实现emf,wmf转换为pdf,eps(修改一行即可进一步支持,svg,png,jpeg等格式)

    Imagemagic 图片处理

    PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSD, PTIF, PWP, R, RAS, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, TIF, TIFF, TILE, TIM, TTC, TTF, TXT, ...

    SWF反编辑软件

    jpeg ,png ,wmf ,emf ,ai ,svg ,swf。 完备的绘图辅助工具 标尺、网格、和多种对齐功能的使用十分简单, 如果您能合理使用,一定能达到事半功倍的效果。 专业脚本编辑器 提供专业脚本编辑器可以帮助用户...

    SoftwareIdeasModeler32-8-55简体中文版 | UML建模软件

    Software Ideas Modeler 是一个功能强大,体积轻巧的工具,用于创建UML图像。 它支持13种UML图像,混合图片,实体关系图...此程序支持导出多种图形格式 (WMF, EMF, PNG, SVG) 和 PDF. 还可以生成项目文档(RTF格式)。

    UML免费建模软件

    Software Ideas Modeler是一个功能强大,体积轻巧的工具,用于创建UML图像。它支持13种URML图像,混合图片,实体关系图... 此程序支持导出多种图形格式 (WMF, EMF, PNG, SVG) 和 PDF. 还可以生成项目文档(RTF格式)。

    cli:WaveDrom的CLI

    是一款很棒的SVG编辑器,可用于将SVG转换为许多其他格式:PDF,PS,EPS,EMF,WMF,PNG。 可以与wavedrom-cli链接以输出这些格式。 这是一个例子: npx wavedrom-cli -i mywave.json5 | inkscape --file - --export...

    ImageMagick 6.4.1-3中文版下载-ImageMagick 6.4.1-3绿色软件下载

    一个免费的创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。 图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线, 多边形,椭圆,曲线,附加到图片伸展旋转。 ImageMagick是...

    ImageMagick 最新版

    PNG32, PNG8, PNM, PPM, PREVIEW, PS, PS2, PS3, PSD, PTIF, PWP, R, RAS, RGB, RGBA, RGBO, RLA, RLE, SCR, SCT, SFW, SGI, SHTML, STEGANO, SUN, SVG, SVGZ, TEXT, TGA, TIF, TIFF, TILE, TIM, TTC, TTF, TXT, ...

    Gnostice XtremeDocumentStudio Delphi 2016 R6 - Berlin

    Currently, it includes VCL components for viewing, printing, and converting PDF, DOCX, DOC, RTF, BMP, JPEG, PNG, WMF, EMF, and single-page and multi-page TIFF. It also has report-export components ...

    Gnostice.eDocEngine.VCL.Professional.v1.03.Cracked-HERETiC

    RTF, HTML, XHTML, EXCEL, TEXT, CSV, Quattro Pro, LOTUS 1-2-3, DIF, SYLK, TIFF, PNG, SVG (XML based vector graphics), JPEG, GIF, BMP, EMF and WMF formats. Metafile, BMP, DIF, SYLK and Text can be ...

    XtremeDocumentStudio Delphi试用版:用于Delphi和C++ Builder下的多格式文档浏览、打印和转换控件

    XtremeDocumentStudio Delphi是一款多格式文档处理控件,开发人员可以利用该产品快速进行文档的浏览、打印、转换,支持的文件格式有PDF, DOCX, BMP, JPEG, PNG, WMF, EMF,TIFF等,同样该产品具有报表导出控件,可以...

    Gnostice EdocEngine VCL v3.0.4 for XE5 Full Source

    eDocEngine can create documents in clipboard, PDF, RTF, HTML, XHTML, EXCEL, TEXT, CSV, Quattro Pro, LOTUS 1-2-3, DIF, SYLK, TIFF, PNG, SVG (XML-based vector graphics), JPEG, GIF, BMP, EMF and WMF ...

    eDocEngine RegPro Full Source v3.0.4.273

    eDocEngine can create documents in clipboard, PDF, RTF, HTML, XHTML, EXCEL, TEXT, CSV, Quattro Pro, LOTUS 1-2-3, DIF, SYLK, TIFF, PNG, SVG (XML-based vector graphics), JPEG, GIF, BMP, EMF and WMF ...

    CAD图纸查看器 CADViewer V9.0

    无需安装Autocad,可以直接查看、打印AutoCAD DWG & DXF, HPGL, SVG and CGM 等等格式文件. 使用技巧: 1、可以按住鼠标右键对图纸进行拖动查看; 2、可以使用鼠标滚轮进行图纸缩放; 3、启用放大镜功能,可以对...

    PDF FLY v6.1d

    软件介绍 用来将Adobe PostScript, Encapsulated PostScript (EPS) 和 Acrobat Portable ...目标文件格式支持矢量: SVG, WMF, EMF, CGM, MIF, EPS, DXF, HPGL;图像: TIFF, GIF, JPEG, PNG以及文本ASCII Tags: PDF FLY

    Edraw Max V7.3

    制作完成的流程图可以导出保存为图像格式,包括BMP/JPEG/PNG/GIF/WMF/EMF/TIFF等格式。 也可以导出为PDF格式、office文件格式、HTML格式和SVG格式。支持FTP上和EMAIL发送。 ,大幅下调所需积分到1积分,为由需要的人...

    Gnostice.eDocEngine.v2.11.VCL.Delphi.BCB.Retail

    这款软件现在支持的文件格式有PDF, RTF, HTML, XHTML, EXCEL, TEXT,CSV, Quattro Pro, LOTUS 1-2-3, DIF, SYLK, TIFF, PNG, SVG (XML based vector graphics), JPEG, GIF, BMP, EMF and WMF. 图元文件, BMP, DIF, ...

    CAD-ABViewer_6.0.0.33_图形查看.rar

    DXF,DWG,PLT,HP,HP1,HPGL,HPGL2,HGL,HG,HPG,RTL,GL2,PLO,SPL,PRN,EMF,WMF,CGM,SVG,BMP,DIB,RLE,JPEG,JPG,ICO,GIF,PCX,TIFF,TIF,WIN,VST,VDA,TGA,ICB,FAX,EPS,RPF,RLA,SGI,RGBA,RGB,BW,PSD,PDD,PPM,PGM,PBM,PNG,CEL,...

Global site tag (gtag.js) - Google Analytics