`
ErHuo
  • 浏览: 21540 次
  • 性别: 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

    总之,通过Java技术,特别是wmf2svg和Batik库,我们可以方便地将WMF和EMF图像转换为SVG和PNG格式,以适应不同的应用场景和平台需求。这种转换方法不仅保持了图像质量,还确保了良好的跨平台兼容性。

    doc/docx转html,以及wmf与emf转jpg,png图片

    本文将深入探讨如何使用Java进行“doc/docx转html”以及“wmf与emf转jpg/png图片”的过程,同时也会提及到batik和poi这两个重要的开源库。 首先,让我们来看“doc/docx转html”。在Java中,Apache POI是一个非常...

    wmf 转png/jpg

    这个文件可能是用Python、Java、C#或其他编程语言编写的,它读取WMF文件,通过SVG作为中间格式,最后生成PNG或JPG图像。用户只需要按照压缩包的说明运行相应的命令,就能批量处理WMF文件,将其转换为目标格式。 在...

    将WMF图片格式转换成PNG

    总的来说,这个项目提供了一种使用Java和Batik库将WMF格式的图片转换为PNG的方法,这对于需要在各种平台和设备上保持一致显示效果的图形处理工作非常有帮助。通过理解这个过程,你可以扩展这个方法,处理更多类型的...

    wmf转svgz、png

    5. 转换过程:WMF转SVGZ和PNG涉及到图像处理软件或在线工具的使用。可以利用Adobe Illustrator、Inkscape这样的专业图形软件进行转换,也可以使用在线转换服务,如 Zamzar 或 Convertio。这些工具通常提供简单的上传...

    java将word转换为html,包含JAVA实现代码与需要jar文件

    在本文中,我们将深入探讨如何使用Java来实现这个功能,包括所需的jar文件和处理Word中的矢量图(WMF和EMF图片)。 首先,我们需要引入一个关键的库,Apache POI,它是一个用于读写Microsoft Office格式文件的Java ...

    word2html.zip

    在Word到HTML的场景下,Batik Transcoder能够将Word文档中的图像(如WMF、EMF格式)转换为Web友好的格式,比如SVG,从而在HTML中正确显示。 "wmf2svg-0.9.0.jar"是另一个关键组件,专门用于将Windows Metafile ...

    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格式)。

    支持复制粘贴word图片的文本编辑器

    这个过程可能包括图片的格式转换(如从EMF、WMF等Word支持的格式转换为常见的JPEG或PNG格式)、大小调整以及与服务器的通信。 压缩包中的“支持复制粘贴word图片的文本编辑器.zip”包含了实现这一功能的所有源代码...

    图片文件格式转换工具

    常见的图片格式有多种,包括BMP、EMF、GIF、JPG、JPEG、PNG以及ICO等。每种格式都有其特点和用途,下面将详细解释这些格式以及转换它们的重要性。 1. BMP(位图):BMP是Windows操作系统中的一个未经压缩的图像文件...

    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等,同样该产品具有报表导出控件,可以...

Global site tag (gtag.js) - Google Analytics