/** * */ public Renderable getRenderable(Image img, OnErrorTypeEnum onErrorType) throws JRException { ImageTypeEnum type = ImageTypeEnum.JPEG; if (img instanceof RenderedImage) { ColorModel colorModel = ((RenderedImage) img).getColorModel(); //if the image has transparency, encode as PNG if (colorModel.hasAlpha() && colorModel.getTransparency() != Transparency.OPAQUE) { type = ImageTypeEnum.PNG; } } return getRenderable(img, type, onErrorType); }
/** * Creates and returns an instance of the JRImageRenderer class after encoding the image object using an image * encoder that supports the supplied image type. * * @param image the java.awt.Image object to wrap into a JRImageRenderer instance * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface * @param onErrorType one of the error type constants defined in the {@link OnErrorTypeEnum}. * @return the image renderer instance */ public Renderable getRenderable(Image image, ImageTypeEnum imageType, OnErrorTypeEnum onErrorType) throws JRException { Renderable result; try { result = new JRImageRenderer(JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(image, imageType)); } catch (Exception e) { result = handleImageError(e, onErrorType); if (log.isDebugEnabled()) { log.debug("handled image error with type " + onErrorType, e); } } return result; }
/** * */ public static ImageTypeEnum getImageTypeValue(byte[] data) { if (JRTypeSniffer.isGIF(data)) { return ImageTypeEnum.GIF; } else if (JRTypeSniffer.isJPEG(data)) { return ImageTypeEnum.JPEG; } else if (JRTypeSniffer.isPNG(data)) { return ImageTypeEnum.PNG; } else if (JRTypeSniffer.isTIFF(data)) { return ImageTypeEnum.TIFF; } return ImageTypeEnum.UNKNOWN; }
/** * */ public SvgDataSniffer.SvgInfo getSvgInfo(byte[] data) { if (JRTypeSniffer.getImageTypeValue(data) == ImageTypeEnum.UNKNOWN) { XmlSniffResult xmlSniff = XmlDataSniffer.sniffXml(data); if (xmlSniff != null) { String rootElement = xmlSniff.getRootElementName(); if (rootElement == null //the sniffer did not determine the root element, trying SVG to be sure || isSvgRootElement(rootElement)) { return getSvgDataSniffer().getSvgInfo(data); } } } return null; }
/** * Creates and returns an instance of the JRImageRenderer class after encoding the image object using an image * encoder that supports the supplied image type. * * @param image the java.awt.Image object to wrap into a JRImageRenderer instance * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface * @param onErrorType one of the error type constants defined in the {@link OnErrorTypeEnum}. * @return the image renderer instance */ public Renderable getRenderable(Image image, ImageTypeEnum imageType, OnErrorTypeEnum onErrorType) throws JRException { byte[] data = null; try { data = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(image, imageType); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("handled image error with type " + onErrorType, e); } return handleImageError(e, onErrorType); } return SimpleDataRenderer.getInstance(data); }
@Override public ImageTypeEnum getImageTypeValue() { if (imageType == null) { try { return JRTypeSniffer.getImageTypeValue(getImageData()); } catch (JRException e) { return ImageTypeEnum.UNKNOWN; } } return imageType; }
protected void doSaveFile(String filename) { if (filename != null) { try { WSClientHelper.getResource(new NullProgressMonitor(), AFileResourcePageContent.this.res, res.getValue(), filename); File file = new File(filename); int dotPos = filename.lastIndexOf("."); //$NON-NLS-1$ String strFilename = filename.substring(0, dotPos); ImageTypeEnum itype = JRTypeSniffer.getImageTypeValue(FileUtils.getBytes(file)); if (itype == ImageTypeEnum.GIF) { file = FileUtils.fileRenamed(file, strFilename, ".gif"); //$NON-NLS-1$ } else if (itype == ImageTypeEnum.JPEG) { file = FileUtils.fileRenamed(file, strFilename, ".jpeg"); //$NON-NLS-1$ } else if (itype == ImageTypeEnum.PNG) { file = FileUtils.fileRenamed(file, strFilename, ".png"); //$NON-NLS-1$ } else if (itype == ImageTypeEnum.TIFF) { file = FileUtils.fileRenamed(file, strFilename, ".tiff"); //$NON-NLS-1$ } ((AFileResource) res).setFile(file); } catch (Exception e1) { UIUtils.showError(e1); } handleFileChange(); } }
@Override public IFile exportToIFile(AFileResource res, ResourceDescriptor rd, String fkeyname, IProgressMonitor monitor) throws Exception { IFile f = super.exportToIFile(res, rd, fkeyname, monitor); if (path == null && f != null) { String filename = f.getFullPath().toOSString(); int dotPos = filename.lastIndexOf("."); if (dotPos >= 0) filename = filename.substring(0, dotPos); ImageTypeEnum itype = JRTypeSniffer.getImageTypeValue(FileUtils.getBytes(f)); if (itype == ImageTypeEnum.UNKNOWN) itype = ImageTypeEnum.PNG; if (itype == ImageTypeEnum.GIF) { f = FileUtils.fileRenamed(f, filename, ".gif", false, monitor); } else if (itype == ImageTypeEnum.JPEG) { f = FileUtils.fileRenamed(f, filename, ".jpeg", false, monitor); } else if (itype == ImageTypeEnum.PNG) { f = FileUtils.fileRenamed(f, filename, ".png", false, monitor); } else if (itype == ImageTypeEnum.TIFF) { f = FileUtils.fileRenamed(f, filename, ".tiff", false, monitor); } fileurimap.put(fkeyname, f); } return f; }
@Override public void test() { super.test(); numberOfPagesTest(1); try { byte[] imageData = JRImageLoader.getInstance(DefaultJasperReportsContext.getInstance()).loadBytesFromAwtImage(image, ImageTypeEnum.JPEG); JRPrintImage jrImage = (JRPrintImage) getElementAt("title.image1", 0); Assert.assertTrue("image data", Arrays.equals(imageData, jrImage.getRenderable().getImageData(DefaultJasperReportsContext.getInstance()))); Assert.assertEquals("scale image", ScaleImageEnum.CLIP, jrImage.getScaleImageValue()); } catch (JRException e) { e.printStackTrace(); Assert.fail(e.getMessage()); } }
@Override public byte[] encode(Image image, ImageTypeEnum imageType) throws JRException { BufferedImage bi = null; if (image instanceof BufferedImage) { bi = (BufferedImage)image; } else { bi = new BufferedImage( image.getWidth(null), image.getHeight(null), // avoid creating JPEG images with transparency that would result // in invalid image files for some viewers (browsers) (imageType == ImageTypeEnum.GIF || imageType == ImageTypeEnum.PNG) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB ); Graphics g = bi.createGraphics(); try { g.drawImage(image, 0, 0, null); } finally { g.dispose(); } } return encode(bi, imageType); }
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); if (PSEUDO_SERIAL_VERSION_UID < JRConstants.PSEUDO_SERIAL_VERSION_UID_4_6_0) { imageTypeValue = ImageTypeEnum.getByValue(imageType); } }
protected void copy(JRPrintImage image) { dataset.finishDataset(); JRComponentElement element = fillContext.getComponentElement(); XYChart xyChart = new XYChartBuilder() .width(element.getWidth()) .height(element.getHeight()) .title(chartTitle == null ? "" : chartTitle) .xAxisTitle(xAxisTitle == null ? "" : xAxisTitle) .yAxisTitle(yAxisTitle == null ? "" : yAxisTitle) .build(); XYStyler styler = xyChart.getStyler(); styler.setLegendPosition(Styler.LegendPosition.InsideNE); styler.setAxisTitlesVisible(true); styler.setDefaultSeriesRenderStyle(XYSeries.XYSeriesRenderStyle.Area); styler.setChartBackgroundColor(element.getBackcolor() == null ? Color.WHITE : element.getBackcolor()); List<Comparable<?>> xySeriesNames = dataset.getXYSeriesNames(); Map<Comparable<?>, XYSeriesData> xySeriesMap = dataset.getXYSeriesMap(); if(xySeriesMap != null && !xySeriesMap.isEmpty()) { int i = 0; for(Comparable<?> name : xySeriesNames) { XYSeriesData data = xySeriesMap.get(name); org.knowm.xchart.XYSeries series = xyChart.addSeries(name.toString(), data.getXData(), data.getYData()); Color color = data.getColor(); if(color != null) { series.setLineColor(color); styler.getSeriesColors()[i] = color; //series.setFillColor(color); } i++; } } try { BufferedImage img = BitmapEncoder.getBufferedImage(xyChart); Renderable renderable = RendererUtil .getInstance(fillContext.getFiller().getJasperReportsContext()) .getRenderable(img, ImageTypeEnum.PNG, OnErrorTypeEnum.ERROR); image.setRenderer(renderable); } catch(Exception e) { throw new JRRuntimeException(e); } }
@Override public Renderable getRenderable( JasperReportsContext jasperReportsContext, JFreeChart chart, ChartHyperlinkProvider chartHyperlinkProvider, Rectangle2D rectangle ) { int dpi = JRPropertiesUtil.getInstance(jasperReportsContext).getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72); double scale = dpi/72d; BufferedImage bi = new BufferedImage( (int) (scale * (int)rectangle.getWidth()), (int) (scale * rectangle.getHeight()), BufferedImage.TYPE_INT_ARGB ); List<JRPrintImageAreaHyperlink> areaHyperlinks = null; Graphics2D grx = bi.createGraphics(); try { grx.scale(scale, scale); if (chartHyperlinkProvider != null && chartHyperlinkProvider.hasHyperlinks()) { areaHyperlinks = ChartUtil.getImageAreaHyperlinks(chart, chartHyperlinkProvider, grx, rectangle); } else { chart.draw(grx, rectangle); } } finally { grx.dispose(); } try { return new SimpleDataRenderer(JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG), areaHyperlinks); } catch (JRException e) { throw new JRRuntimeException(e); } }
@Override public ImageTypeEnum getImageTypeValue() { return ImageTypeEnum.PNG; }
@Override public byte[] getImageData(JasperReportsContext jasperReportsContext) throws JRException { Dimension2D dimension = getDimension(jasperReportsContext); if (dimension == null) { throw new JRException( EXCEPTION_MESSAGE_KEY_DIMENSION_NULL_NOT_ALLOWED, (Object[])null ); } int dpi = getImageDataDPI(jasperReportsContext); double scale = dpi/72d; ImageTypeEnum imageType = getImageTypeValue(); BufferedImage bi = new BufferedImage( (int) (scale * dimension.getWidth()), (int) (scale * dimension.getHeight()), // avoid creating JPEG images with transparency that would result // in invalid image files for some viewers (browsers) (imageType == ImageTypeEnum.GIF || imageType == ImageTypeEnum.PNG) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB ); Graphics2D g = createGraphics(bi); try { g.scale(scale, scale); Color backcolor = getBackcolor(); if (backcolor != null) { g.setColor(backcolor); g.fillRect(0, 0, (int)dimension.getWidth(), (int)dimension.getHeight()); } render(jasperReportsContext, g, new Rectangle((int)dimension.getWidth(), (int)dimension.getHeight())); } finally { g.dispose(); } return JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, getImageTypeValue()); }
@Override public ImageTypeEnum getImageTypeValue() { return ImageTypeEnum.getByValue(getImageType()); }
@Override public ImageTypeEnum getImageTypeValue() { return ImageTypeEnum.getByValue(renderable.getImageType()); }
@Override public byte[] encode(BufferedImage bi, ImageTypeEnum imageType) throws JRException { String formatName = null; switch (imageType) { case GIF : { formatName = "gif"; break; } case PNG : { formatName = "png"; break; } case TIFF : { formatName = "tiff"; break; } case JPEG : case UNKNOWN : default: { formatName = "jpeg"; break; } } boolean success = false; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { success = ImageIO.write(bi, formatName, baos); } catch (IOException e) { throw new JRException(e); } if (!success) { throw new JRException( EXCEPTION_MESSAGE_KEY_IMAGE_WRITER_NOT_FOUND, new Object[]{formatName}); } return baos.toByteArray(); }
@Override public ImageTypeEnum getImageTypeValue() { return imageTypeValue; }
private InternalImageProcessorResult processImageClip(Graphics2DRenderable renderer) throws JRException { int normalWidth = availableImageWidth; int normalHeight = availableImageHeight; Dimension2D dimension = renderer instanceof DimensionRenderable ? ((DimensionRenderable)renderer).getDimension(jasperReportsContext) : null; if (dimension != null) { normalWidth = (int) dimension.getWidth(); normalHeight = (int) dimension.getHeight(); } int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72); double scale = dpi/72d; BufferedImage bi = new BufferedImage( (int)(scale * availableImageWidth), (int)(scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB ); Graphics2D grx = bi.createGraphics(); try { grx.scale(scale, scale); grx.clip( new Rectangle( 0, 0, availableImageWidth, availableImageHeight ) ); renderer.render( jasperReportsContext, grx, new Rectangle( (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageWidth - normalWidth)), (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageHeight - normalHeight)), normalWidth, normalHeight ) ); } finally { grx.dispose(); } byte[] imageData = null; int topOffset = 0; int leftOffset = 0; int bottomOffset = 0; int rightOffset = 0; topOffset = topPadding; leftOffset = leftPadding; bottomOffset = bottomPadding; rightOffset = rightPadding; imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG); return new InternalImageProcessorResult(imageData, topOffset, leftOffset, bottomOffset, rightOffset); }
private InternalImageProcessorResult processImageClip(Graphics2DRenderable renderer) throws JRException { int normalWidth = availableImageWidth; int normalHeight = availableImageHeight; Dimension2D dimension = renderer instanceof DimensionRenderable ? ((DimensionRenderable)renderer).getDimension(jasperReportsContext) : null; if (dimension != null) { normalWidth = (int) dimension.getWidth(); normalHeight = (int) dimension.getHeight(); } int dpi = getPropertiesUtil().getIntegerProperty(Renderable.PROPERTY_IMAGE_DPI, 72); double scale = dpi/72d; BufferedImage bi = new BufferedImage( (int)(scale * availableImageWidth), (int)(scale * availableImageHeight), BufferedImage.TYPE_INT_ARGB ); Graphics2D grx = bi.createGraphics(); try { grx.scale(scale, scale); grx.clip( new Rectangle( 0, 0, availableImageWidth, availableImageHeight ) ); renderer.render( jasperReportsContext, grx, new Rectangle( (int) (ImageUtil.getXAlignFactor(imageElement) * (availableImageWidth - normalWidth)), (int) (ImageUtil.getYAlignFactor(imageElement) * (availableImageHeight - normalHeight)), normalWidth, normalHeight ) ); } finally { grx.dispose(); } byte[] imageData = null; int topOffset = 0; int leftOffset = 0; int bottomOffset = 0; int rightOffset = 0; // topOffset = topPadding; // leftOffset = leftPadding; // bottomOffset = bottomPadding; // rightOffset = rightPadding; imageData = JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG); return new InternalImageProcessorResult(imageData, topOffset, leftOffset, bottomOffset, rightOffset); }
protected InternalImageProcessorResult(byte[] imageData, Dimension2D dimension, ImageTypeEnum imageType) { this.imageData = imageData; this.dimension = dimension; this.imageType = imageType; }
@Override public byte[] getData(JasperReportsContext jasperReportsContext) throws JRException { Dimension2D dimension = getDimension(jasperReportsContext); if (dimension == null) { throw new JRException( EXCEPTION_MESSAGE_KEY_DIMENSION_NULL_NOT_ALLOWED, (Object[])null ); } int dpi = getImageDataDPI(jasperReportsContext); double scale = dpi/72d; BufferedImage bi = new BufferedImage( (int) (scale * dimension.getWidth()), (int) (scale * dimension.getHeight()), BufferedImage.TYPE_INT_ARGB // always produce PNGs with transparency ); Graphics2D g = createGraphics(bi); try { g.scale(scale, scale); Color backcolor = getBackcolor(); if (backcolor != null) { g.setColor(backcolor); g.fillRect(0, 0, (int)dimension.getWidth(), (int)dimension.getHeight()); } render(jasperReportsContext, g, new Rectangle((int)dimension.getWidth(), (int)dimension.getHeight())); } finally { g.dispose(); } return JRImageLoader.getInstance(jasperReportsContext).loadBytesFromAwtImage(bi, ImageTypeEnum.PNG); // always produce PNGs with transparency }
/** * @deprecated Replaced by {@link JRTypeSniffer#getImageTypeValue(byte[])}. */ public ImageTypeEnum getImageTypeValue();
/** * */ public byte[] encode(Image image, ImageTypeEnum imageType) throws JRException;
/** * */ public abstract byte[] encode(BufferedImage bi, ImageTypeEnum imageType) throws JRException;
/** * Encoding the image object using an image encoder that supports the supplied image type. * * @param image the java.awt.Image object to encode * @param imageType the type of the image as specified by one of the constants defined in the JRRenderable interface * @return the encoded image data */ public byte[] loadBytesFromAwtImage(Image image, ImageTypeEnum imageType) throws JRException { return imageEncoder.encode(image, imageType); }