2012-06-27 61 views
3

我轉換後的圖像質量非常低。我嘗試過使用setQuality方法,但它似乎沒有改變任何東西。在Batik中使用SVGConverter將svg轉換爲png時圖像質量差

這是SVG轉換成PNG代碼的一部分:

SVGConverter svgConverter = new SVGConverter(); 
String[] sources = { "C:/imageData.svg" }; 
svgConverter.setSources(sources); 
svgConverter.setDestinationType(DestinationType.PNG); 
svgConverter.setDst(new File("C:/image.png")); 

有誰知道我怎麼可以使圖像質量更好?

回答

4

我(用字節數組)轉換SVG至PNG時,使用此代碼:

private byte[] renderPng(byte[] svgBytes) { 
    try { 
     TranscoderInput transcoderInput = new TranscoderInput(new ByteArrayInputStream(svgBytes)); 
     ByteArrayOutputStream output = new ByteArrayOutputStream(); 
     TranscoderOutput transcoderOutput = new TranscoderOutput(output); 

     Transcoder transcoder = null; 
     String type = "png"; 
     if(type.equalsIgnoreCase("png")) { 
      transcoder = new PNGTranscoder() 
      { 
       @Override 
       protected ImageRenderer createRenderer() 
       { 
        ImageRenderer r = super.createRenderer(); 

        RenderingHints rh = r.getRenderingHints(); 

        rh.add(new RenderingHints(RenderingHints.KEY_ALPHA_INTERPOLATION, 
         RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY)); 
        rh.add(new RenderingHints(RenderingHints.KEY_INTERPOLATION, 
         RenderingHints.VALUE_INTERPOLATION_BICUBIC)); 

        rh.add(new RenderingHints(RenderingHints.KEY_ANTIALIASING, 
         RenderingHints.VALUE_ANTIALIAS_ON)); 

        rh.add(new RenderingHints(RenderingHints.KEY_COLOR_RENDERING, 
         RenderingHints.VALUE_COLOR_RENDER_QUALITY)); 
        rh.add(new RenderingHints(RenderingHints.KEY_DITHERING, 
         RenderingHints.VALUE_DITHER_DISABLE)); 

        rh.add(new RenderingHints(RenderingHints.KEY_RENDERING, 
         RenderingHints.VALUE_RENDER_QUALITY)); 

        rh.add(new RenderingHints(RenderingHints.KEY_STROKE_CONTROL, 
         RenderingHints.VALUE_STROKE_PURE)); 

        rh.add(new RenderingHints(RenderingHints.KEY_FRACTIONALMETRICS, 
         RenderingHints.VALUE_FRACTIONALMETRICS_ON)); 
        rh.add(new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, 
         RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)); 

        r.setRenderingHints(rh); 

        return r; 
       } 
      }; 

      transcoder.addTranscodingHint(PNGTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE); 
     } else { 
      transcoder = new JPEGTranscoder(); 

      Float jpegQuality = new Float(0.95); 
      // KEY_WIDTH - seems to pick it up just fine from the SVG charts. Set to 560 otherwise. 
      // KEY_QUALITY 0-1.0 with 1.0 being No Loss. Value must be of type Float. 0.95 is 30% smaller and looks great. 
      transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, jpegQuality); 
     } 

     // NOTE: for linux you need Java 1.4.1+ AND the headless environment (e.g. export JAVA_OPTS='-Djava.awt.headless=true'). 
     try { 
      transcoder.transcode(transcoderInput, transcoderOutput); 
     } 
     catch(Exception e) { 
      logger.error("SVG To Raster response transcode exception", e); 
      if(output != null) { 
       output.close(); 
      } 
      throw(new RuntimeException("SVG To Raster Filter Response Stream Exception", e)); 
     } 

     if(output != null) { 
      output.flush(); 
      output.close(); 
     } 

     transcoderInput = null; 
     transcoderOutput = null; 
     transcoder = null; 

     return output.toByteArray(); 
    } catch (Exception exc) { 
     logger.error("Error in rendering png method", exc); 
    } 
    return new byte[0]; 
} 

此外,在您的SVG文件的clipPath元素應該包含這個屬性,否則將有奇怪的文物:

<clipPath shape-rendering="geometricPrecision" ... > 
0

試試這個....

jpeg.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(keyQuality)); 
jpeg.addTranscodingHint(JPEGTranscoder.KEY_HEIGHT, height*scalePercentage); 
jpeg.addTranscodingHint(JPEGTranscoder.KEY_WIDTH, width*scalePercentage); 
jpeg.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, 1.0f); 
jpeg.addTranscodingHint(JPEGTranscoder.KEY_BACKGROUND_COLOR, Color.WHITE); 

你需要增加高度和寬度l ike set keyQuality 1.0f。

如果height = 200將高度增加200 * 3(height * scalePercentage)。類似寬度也

相關問題