当前位置:编程学习 > JAVA >>

求牛人提供处理svg分辨率的java代码实例

--------------------编程问答-------------------- 你用 ImageIO.read 读取图片时,图片的色彩模型会被保存下来。避免这种问题,新建一个 BufferedImage 来画加载的图片,然后 SVG 图形:
BufferedImage source = ImageIO.read(new File("imgToDrawOnto.png"));
BufferedImage newImage = new BufferedImage(
    source.getWidth(), 
    source.getHeight(), 
    BufferedImage.TYPE_INT_ARGB);

Graphics2D g = (Graphics2D)newImage.getGraphics();

g.drawImage(source, 0, 0, null);
g.drawImage(circleImg, 100, 100, null);

--------------------编程问答--------------------
引用 1 楼 defonds 的回复:
你用 ImageIO.read 读取图片时,图片的色彩模型会被保存下来。避免这种问题,新建一个 BufferedImage 来画加载的图片,然后 SVG 图形:
BufferedImage source = ImageIO.read(new File("imgToDrawOnto.png"));
BufferedImage newImage = new BufferedImage(
    source.getWidth(), 
    source.getHeight(), 
    BufferedImage.TYPE_INT_ARGB);

Graphics2D g = (Graphics2D)newImage.getGraphics();

g.drawImage(source, 0, 0, null);
g.drawImage(circleImg, 100, 100, null);


版主,你没理解我的意思呢,我的需求不是将png转换成svg。。
我原意是wmf--》svg--》png。。第一部分我省略了,wmf到svg部分生成的svg文件分辨率过大了。
下面是我的代码:
/**
 * 将wmf转换为svg
 * 
 * @param src
 * @param dest
 */
public static void wmfToSvg(String src, String dest) throws Exception {
boolean compatible = false;
try {
InputStream in = new FileInputStream(src);
WmfParser parser = new WmfParser();

final SvgGdi gdi = new SvgGdi(compatible);
parser.parse(in, gdi);

Document doc = gdi.getDocument();
OutputStream out = new FileOutputStream(dest);
if (dest.endsWith(".svgz")) {
out = new GZIPOutputStream(out);
}

output(doc, out);
} catch (Exception e) {
throw e;
}
}

private static void output(Document doc, OutputStream out) 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));
out.flush();
out.close();
out = null;
} --------------------编程问答-------------------- 或者谁有svg-->png的好方法啊,,
我下面的方法爆内存了:
/**
 * 将svg图片转成png图片
 * 
 * @param filePath
 * @throws Exception
 */
public static void svgToPng(String svgPath, String pngFile) throws Exception {
File svg = new File(svgPath);
FileInputStream wmfStream = new FileInputStream(svg);
ByteArrayOutputStream imageOut = new ByteArrayOutputStream();
int noOfByteRead = 0;
while ((noOfByteRead = wmfStream.read()) != -1) {
imageOut.write(noOfByteRead);
}
imageOut.flush();
imageOut.close();
wmfStream.close();

ByteArrayOutputStream jpg = new ByteArrayOutputStream();
FileOutputStream jpgOut = new FileOutputStream(pngFile);

byte[] bytes = imageOut.toByteArray();
PNGTranscoder t = new PNGTranscoder();
TranscoderInput in = new TranscoderInput(new ByteArrayInputStream(bytes));
TranscoderOutput out = new TranscoderOutput(jpg);
t.transcode(in, out);
jpgOut.write(jpg.toByteArray());
jpgOut.flush();
jpgOut.close();
imageOut = null;
jpgOut = null;
 }
[/code] --------------------编程问答-------------------- 问题自行解决了,不过也很感谢版主的回复,分数全给您了。 --------------------编程问答-------------------- 解决方案能否跟帖分享一下?替以后遇到同样问题搜索到这个页面的同学先谢谢你了
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,