By Date: <-- -->
By Thread: <-- -->

Grainy images



Title: Message
SOme of the user (Bill ensley) pointed to Jpedal as a good library for extracting images PDFs.  I managed to get my page xtracted as an image, but when it came out it was clear but has this strange background "fuzz"  I was wondering if any of you had any idea.  here is my java code. I know this is not an itext problem but maybe your users know whats happening here.
 
private void generatePNGfromPDF(PdfDecoder decoder, int PageNumber, double width, double height, String formname){
    //int size = 100;
    try
    {
    //decoder.decodePage(PageNumber);
    //decoder.setPageParameters(1,PageNumber); //values scaling (1=100%). page number
    decoder.useHiResScreenDisplay(true);
   
    int dpi=300;
    decoder.setExtractionMode(32, dpi,2);
    BufferedImage PDF = decoder.getPageAsImage(PageNumber);
    width = PDF.getWidth() * .75;
    height = PDF.getHeight() * .75;
 
    GraphicsConfiguration gc = getDefaultConfiguration();
    final BufferedImage PNG = getScaledInstance(PDF, (int)Math.round(width), (int)Math.round(height), gc);
    String fileName = jTextAreaTargetDirectoryPath.getText() + "\\" + formname + "-" + PageNumber + ".png";
    ImageIO.write(PNG, "png", new File( fileName ));
    decoder.flushObjectValues(true);
    }
    catch(Exception ex){
        flogger.log(Level.SEVERE, "Cannot generate image from page.", ex);
    }  
   
}
private static GraphicsConfiguration getDefaultConfiguration() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        return gd.getDefaultConfiguration();
    }
private static BufferedImage toCompatibleImage(BufferedImage image, GraphicsConfiguration gc) {
        if (gc == null)
            gc = getDefaultConfiguration();
        int w = image.getWidth();
        int h = image.getHeight();
        int transparency = image.getColorModel().getTransparency();
        BufferedImage result = gc.createCompatibleImage(w, h, transparency);
        Graphics2D g2 = result.createGraphics();
        g2.drawRenderedImage(image, null);
        g2.dispose();
        return result;
    }    
private static BufferedImage copy(BufferedImage source, BufferedImage target) {
        Graphics2D g2 = target.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
     g2.setRenderingHint(RenderingHints.KEY_DITHERING,
    RenderingHints.VALUE_DITHER_DISABLE);
 g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    RenderingHints.VALUE_ANTIALIAS_OFF);       
  g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
    RenderingHints.VALUE_FRACTIONALMETRICS_ON);      
        double scalex = (double) target.getWidth()/ source.getWidth();
        double scaley = (double) target.getHeight()/ source.getHeight();
        AffineTransform xform = AffineTransform.getScaleInstance(scalex, scaley);
        g2.drawRenderedImage(source, xform);
        g2.dispose();
        return target;
    }
private static BufferedImage getScaledInstance(BufferedImage image, int width, int height, GraphicsConfiguration gc) {
        if (gc == null)
            gc = getDefaultConfiguration();
        int transparency = image.getColorModel().getTransparency();
        return copy(image, gc.createCompatibleImage(width, height, transparency));
    }
private PdfDecoder openpdf(Object file){
    //open pdf using jpedal
    PdfDecoder decoder = new PdfDecoder();
    try
    {
        decoder.openPdfFile(file.toString());
    }
    catch(org.jpedal.exception.PdfException ex){
        flogger.log(Level.SEVERE, "Cannot open pdf.", ex);
    }
     return decoder; 
}
 

Richard Braman
mailto:rbraman (at) taxcodesoftware.org
561.748.4002 (voice)

http://www.taxcodesoftware.org
Free Open Source Tax Software