List of usage examples for javax.imageio ImageIO write
public static boolean write(RenderedImage im, String formatName, OutputStream output) throws IOException
From source file:com.truven.report.reporter.Reporter.java
/** * Save screenshot thumb./*from www. j a v a 2 s.c o m*/ * * @param screenShotFile the screen shot file * @return the string */ private String saveScreenshotThumb(final String screenShotFile) { final int thumbWidth = 150; final int thumbHeight = 100; String screenShotThumb = "images" + File.separator + screenShotFile + "_Thumb.png"; try { String screenShotOriginalFile = builder.getReportFolderLocation() + File.separator + "images" + File.separator + screenShotFile; BufferedImage img = ImageIO.read(new File(screenShotOriginalFile)); BufferedImage thumb = Scalr.resize(img, Method.SPEED, thumbWidth, thumbHeight, Scalr.OP_ANTIALIAS, Scalr.OP_BRIGHTER); ImageIO.write(thumb, "png", new File(screenShotOriginalFile + "_Thumb.png")); } catch (IOException e) { e.printStackTrace(); } return screenShotThumb; }
From source file:io.github.karols.hocr4j.PageRenderer.java
/** * Renders this page on a blank image and saves it as a PNG file. * * @param page page to render//from ww w .j a v a2s. co m * @param outputFile output file * @throws IOException */ public void renderOnBlank(@Nonnull Page page, @Nonnull File outputFile) throws IOException { BufferedImage img = renderOnBlank(page); ImageIO.write(img, imageFormat, outputFile); }
From source file:com.hygenics.imaging.ImageCleanup.java
public byte[] reaverage(double factor, byte[] ibytes) { // tone red coloring BufferedImage color = BufferImage(ibytes); if (color != null) { BufferedImage averaged = new BufferedImage(color.getWidth(), color.getHeight(), BufferedImage.TYPE_INT_RGB); for (int i = 0; i < color.getWidth(); i++) { for (int j = 0; j < color.getHeight(); j++) { Color c = new Color(color.getRGB(i, j)); averaged.setRGB(i, j,/*from w w w.ja v a 2 s . co m*/ new Color((int) Math.round(c.getRed() / factor), c.getGreen(), c.getBlue()).getRGB()); } } ByteArrayOutputStream bos = new ByteArrayOutputStream(); // convert to jpeg for compression try { // use apache to read to a buffered image with certain metadata // and then convert to a jpg ImageIO.write(averaged, "jpg", bos); return ibytes = bos.toByteArray(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return ibytes; }
From source file:gr.abiss.calipso.fs.FilePersistenceService.java
public default FileDTO scaleFile(BufferedImage img, String contentType, int maxWidth, int maxHeight) throws IOException { BufferedImage scaled = Scalr.resize(img, Scalr.Method.SPEED, Scalr.Mode.FIT_TO_WIDTH, maxWidth, maxHeight, Scalr.OP_ANTIALIAS);/*from w ww. j a va 2 s .c o m*/ ByteArrayOutputStream os = new ByteArrayOutputStream(); ImageIO.write(scaled, getImageIoFormat(contentType), os); FileDTO scaledFile = new FileDTO.Builder().contentLength(os.size()).contentType(contentType) .in(new ByteArrayInputStream(os.toByteArray())).build(); os.close(); return scaledFile; }
From source file:com.ttech.cordovabuild.domain.application.source.ApplicationSourceFactoryImpl.java
private AssetRef getIconAssetRef(Path sourcePath, DomEditor.IconConfig iconConfig) { if (iconConfig == null) return null; Path path = sourcePath.resolve(iconConfig.getSrc()).normalize(); if (isSubPath(path, sourcePath) && !path.equals(sourcePath) && path.getFileName().toString().toLowerCase().endsWith(".png")) { try (InputStream fs = new FileInputStream(path.toFile())) { if (iconConfig.getHeight() != null || iconConfig.getWidth() != null) { BufferedImage bufferedImage = ImageIO.read(fs); bufferedImage = scaleTo(bufferedImage, iconConfig.getHeight(), iconConfig.getWidth()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", outputStream); return assetService.save(new ByteArrayInputStream(outputStream.toByteArray()), "image/png"); } else return assetService.save(fs, "image/png"); } catch (IOException e) { LOGGER.warn("image file cannot be read"); }/*from w w w. j av a 2 s. c o m*/ } return null; }
From source file:org.drools.planner.benchmark.core.statistic.PlannerStatistic.java
private void writeBestScoreSummaryChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); for (SolverBenchmark solverBenchmark : plannerBenchmark.getSolverBenchmarkList()) { ScoreDefinition scoreDefinition = solverBenchmark.getSolverConfig().getScoreDirectorFactoryConfig() .buildScoreDefinition(); for (SingleBenchmark singleBenchmark : solverBenchmark.getSingleBenchmarkList()) { if (singleBenchmark.isSuccess()) { Score score = singleBenchmark.getScore(); Double scoreGraphValue = scoreDefinition.translateScoreToGraphValue(score); String solverLabel = solverBenchmark.getName(); if (solverBenchmark.isRankingBest()) { solverLabel += " (winner)"; }//from www . j av a2s . com String planningProblemLabel = singleBenchmark.getProblemBenchmark().getName(); dataset.addValue(scoreGraphValue, solverLabel, planningProblemLabel); } } } CategoryAxis xAxis = new CategoryAxis("Data"); xAxis.setCategoryMargin(0.40); NumberAxis yAxis = new NumberAxis("Score"); BarRenderer renderer = new BarRenderer(); ItemLabelPosition positiveItemLabelPosition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER); renderer.setBasePositiveItemLabelPosition(positiveItemLabelPosition); ItemLabelPosition negativeItemLabelPosition = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER); renderer.setBaseNegativeItemLabelPosition(negativeItemLabelPosition); renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderer.setBaseItemLabelsVisible(true); CategoryPlot plot = new CategoryPlot(dataset, xAxis, yAxis, renderer); plot.setOrientation(PlotOrientation.VERTICAL); JFreeChart chart = new JFreeChart("Best score summary (higher score is better)", JFreeChart.DEFAULT_TITLE_FONT, plot, true); BufferedImage chartImage = chart.createBufferedImage(1024, 768); bestScoreSummaryFile = new File(plannerBenchmark.getBenchmarkReportDirectory(), "bestScoreSummary.png"); OutputStream out = null; try { out = new FileOutputStream(bestScoreSummaryFile); ImageIO.write(chartImage, "png", out); } catch (IOException e) { throw new IllegalArgumentException("Problem writing bestScoreSummaryFile: " + bestScoreSummaryFile, e); } finally { IOUtils.closeQuietly(out); } }
From source file:net.sf.dynamicreports.jasper.builder.JasperConcatenatedReportBuilder.java
public JasperConcatenatedReportBuilder toPng(OutputStream outputStream, float zoom) throws DRException { Validate.notNull(outputStream, "outputStream must not be null"); Validate.isTrue(zoom > 0, "zoom must be > 0"); int maxWidth = 0; int maxHeight = 0; for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) { int pages = jasperPrint.getPages().size(); int pageWidth = (int) (jasperPrint.getPageWidth() * zoom); maxWidth += pageWidth * pages + (pages - 1) + 2; int height = (int) (jasperPrint.getPageHeight() * zoom) + 2; if (height > maxHeight) { maxHeight = height;/*from w w w. j a va2s . c o m*/ } } Image pageImage = new BufferedImage(maxWidth, maxHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = (Graphics2D) pageImage.getGraphics(); g2d.setColor(Color.LIGHT_GRAY); g2d.fill(new Rectangle2D.Float(1, 1, maxWidth - 1, maxHeight - 1)); int offset = 1; for (JasperPrint jasperPrint : jasperReportHandler.getPrintList()) { int pageWidth = (int) (jasperPrint.getPageWidth() * zoom); for (int i = 0; i < jasperPrint.getPages().size(); i++) { try { JRGraphics2DExporter exporter = new JRGraphics2DExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics()); exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_X, offset); exporter.setParameter(JRGraphics2DExporterParameter.OFFSET_Y, 1); exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(i)); exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float(zoom)); exporter.exportReport(); offset += pageWidth + 1; } catch (JRException e) { throw new DRException(e); } } } try { ImageIO.write((RenderedImage) pageImage, "png", outputStream); } catch (IOException e) { throw new DRException(e); } return this; }
From source file:controllers.PictureController.java
private void reziseImage(String fileLocation) { BufferedImage img = null;/* w ww. j a va 2 s. com*/ String extension = FilenameUtils.getExtension(fileLocation); String fileName = FilenameUtils.getName(fileLocation); File file = new File(fileLocation); try { img = ImageIO.read(file); BufferedImage webPic = resize(img, 1024); BufferedImage thumbnail = resize(img, Method.SPEED, Mode.FIT_TO_WIDTH, 150, 100, OP_ANTIALIAS); ImageIO.write(webPic, extension, new File("public/pictures/" + fileName)); ImageIO.write(thumbnail, extension, new File("public/pictures/thumbs/" + fileName)); } catch (IOException e) { } }
From source file:org.brunocvcunha.instagram4j.requests.InstagramUploadPhotoRequest.java
private byte[] bufferedImageToByteArray(BufferedImage image) throws IOException { try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(image, "jpg", baos); baos.flush();// w ww . ja va 2 s . co m return baos.toByteArray(); } }
From source file:com.liusoft.dlog4j.action.PhotoAction.java
/** * // w ww . ja v a2s . c o m * @param ctx * @param imgURL * @param orient * @return * @throws IOException */ protected boolean rotate(HttpContext ctx, String imgURL, int orient) throws IOException { PhotoSaver saver = this.getPhotoSaver(); InputStream inImg = saver.read(ctx, imgURL); BufferedImage old_img = (BufferedImage) ImageIO.read(inImg); int width = old_img.getWidth(); int height = old_img.getHeight(); BufferedImage new_img = new BufferedImage(height, width, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = new_img.createGraphics(); AffineTransform origXform = g2d.getTransform(); AffineTransform newXform = (AffineTransform) (origXform.clone()); // center of rotation is center of the panel double radian = 0; double xRot = 0; double yRot = 0; switch (orient) { case 3: radian = 180.0; xRot = width / 2.0; yRot = height / 2.0; case 6: radian = 90.0; xRot = height / 2.0; yRot = xRot; break; case 8: radian = 270.0; xRot = width / 2.0; yRot = xRot; break; default: return false; } newXform.rotate(Math.toRadians(radian), xRot, yRot); g2d.setTransform(newXform); // draw image centered in panel g2d.drawImage(old_img, 0, 0, null); // Reset to Original g2d.setTransform(origXform); OutputStream out = saver.write(ctx, imgURL); try { ImageIO.write(new_img, "JPG", out); } finally { out.close(); } return true; }