List of usage examples for javax.imageio ImageIO read
public static BufferedImage read(ImageInputStream stream) throws IOException
From source file:com.dbi.jmmerge.MapController.java
@RequestMapping(value = "/maps/{server}/{dimension}/{level}/{x},{y}.png", produces = "image/png") public byte[] grabImage(@PathVariable("server") String server, @PathVariable("dimension") String dimension, @PathVariable("level") String level, @PathVariable("x") String x, @PathVariable("y") String y) throws IOException { File imageFile = buildFileByElements(new File(storageDir), server, "DIM" + dimension, level, x + "," + y + "-opaque.png"); if (!imageFile.exists()) { File originalImageFile = buildFileByElements(new File(storageDir), server, "DIM" + dimension, level, x + "," + y + ".png"); if (!originalImageFile.exists()) { LOG.error("Could not find file: " + originalImageFile.getAbsolutePath()); throw new IllegalArgumentException("Could not find file:" + originalImageFile.getName()); }/*from w w w . j a v a2 s. c om*/ ImageIO.write(PNGMerge.blackBackgroundedImage(ImageIO.read(originalImageFile)), "png", imageFile); } ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(new FileInputStream(imageFile), baos); return baos.toByteArray(); }
From source file:com.impetus.client.crud.datatypes.ByteDataTest.java
/** * On insert image in cassandra blob object * /*from w w w . j a v a2 s .c o m*/ * @throws Exception * the exception */ @Test public void onInsertBlobImageCassandra() throws Exception { PersonCassandra personWithKey = new PersonCassandra(); personWithKey.setPersonId("111"); BufferedImage originalImage = ImageIO.read(new File("src/test/resources/nature.jpg")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, "jpg", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); personWithKey.setA(imageInByte); em.persist(personWithKey); em.clear(); String qry = "Select p from PersonCassandra p where p.personId = 111"; Query q = em.createQuery(qry); List<PersonCassandra> persons = q.getResultList(); PersonCassandra person = persons.get(0); InputStream in = new ByteArrayInputStream(person.getA()); BufferedImage bImageFromConvert = ImageIO.read(in); ImageIO.write(bImageFromConvert, "jpg", new File("src/test/resources/nature-test.jpg")); Assert.assertNotNull(person.getA()); Assert.assertEquals(new File("src/test/resources/nature.jpg").getTotalSpace(), new File("src/test/resources/nature-test.jpg").getTotalSpace()); Assert.assertEquals(String.valueOf(Hex.encodeHex((byte[]) imageInByte)), String.valueOf(Hex.encodeHex((byte[]) person.getA()))); }
From source file:net.sourceforge.subsonic.controller.AvatarUploadController.java
private void createAvatar(String fileName, byte[] data, String username, Map<String, Object> map) throws IOException { BufferedImage image;//w ww. j a v a 2 s . c o m try { image = ImageIO.read(new ByteArrayInputStream(data)); if (image == null) { throw new Exception( "Failed to decode incoming image: " + fileName + " (" + data.length + " bytes)."); } int width = image.getWidth(); int height = image.getHeight(); String mimeType = StringUtil.getMimeType(FilenameUtils.getExtension(fileName)); // Scale down image if necessary. if (width > MAX_AVATAR_SIZE || height > MAX_AVATAR_SIZE) { double scaleFactor = (double) MAX_AVATAR_SIZE / (double) Math.max(width, height); height = (int) (height * scaleFactor); width = (int) (width * scaleFactor); image = CoverArtController.scale(image, width, height); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(image, "jpeg", out); data = out.toByteArray(); mimeType = StringUtil.getMimeType("jpeg"); map.put("resized", true); } Avatar avatar = new Avatar(0, fileName, new Date(), mimeType, width, height, data); settingsService.setCustomAvatar(avatar, username); LOG.info("Created avatar '" + fileName + "' (" + data.length + " bytes) for user " + username); } catch (Exception x) { LOG.warn("Failed to upload personal image: " + x, x); map.put("error", x); } }
From source file:edu.harvard.mcz.imagecapture.WhatsThisImageDialog.java
/** Constructor with an image File to display as a parameter. Will * display a broken image icon if File can't be read as an * image./*from w ww .java 2s . com*/ * * @param owner the parent frame for this dialog. * @param imageFileToShow the image file of unknown nature to display. */ public WhatsThisImageDialog(Frame owner, File imageFileToShow) { super(owner, true); thisDialog = this; initialize(); BufferedImage image; try { image = ImageIO.read(imageFileToShow); this.setImage(image); } catch (IOException e) { log.error("Unable to open selected image " + imageFileToShow.getName()); log.debug(e); URL errorFilename = this.getClass() .getResource("/edu/harvard/mcz/imagecapture/resources/unableToLoadImage.jpg"); try { this.setImage(ImageIO.read(errorFilename)); } catch (IOException e1) { log.error("Unable to open resource image"); log.error(e1); } } }
From source file:com.qwazr.crawler.web.driver.BrowserDriver.java
final public BufferedImage getScreenshot() throws IOException { if (!(driver instanceof TakesScreenshot)) throw new WebDriverException("This browser driver does not support screenshot"); TakesScreenshot takesScreenshot = (TakesScreenshot) driver; byte[] data = takesScreenshot.getScreenshotAs(OutputType.BYTES); return ImageIO.read(new ByteArrayInputStream(data)); }
From source file:com.liud.dailynote.ThumbnailatorTest.java
private void testYS8() throws IOException { String result = "src/main/resources/images/"; OutputStream os = new FileOutputStream(result + "sijili_out.jpg"); Image image = ImageIO.read(new File(result + "sijili.jpg")); BufferedImage bufferedImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); bufferedImage.getGraphics().drawImage(image.getScaledInstance(100, 100, image.SCALE_SMOOTH), 0, 0, null); ImageIO.write(bufferedImage, "jpg", os); os.close();/*from ww w . jav a 2 s . c o m*/ }
From source file:com.github.zhanhb.ckfinder.connector.utils.ImageUtils.java
/** * Uploads image and if the image size is larger than maximum allowed it * resizes the image.//from ww w . j a va 2s.c o m * * @param part servlet part * @param file file name * @param fileName name of file * @param context ckfinder context * @throws IOException when IO Exception occurs. */ public static void createTmpThumb(InputStreamSource part, Path file, String fileName, CKFinderContext context) throws IOException { BufferedImage image; try (InputStream stream = part.getInputStream()) { image = ImageIO.read(stream); if (image == null) { throw new IOException("Wrong file"); } } ImageProperties imageProperties = context.getImage(); Dimension dimension = createThumbDimension(image, imageProperties.getMaxWidth(), imageProperties.getMaxHeight()); if (dimension.width == 0 || dimension.height == 0 || (image.getHeight() <= dimension.height && image.getWidth() <= dimension.width)) { try (InputStream stream = part.getInputStream()) { Files.copy(stream, file, StandardCopyOption.REPLACE_EXISTING); } } else { resizeImage(image, dimension.width, dimension.height, imageProperties.getQuality(), file); } if (log.isTraceEnabled()) { log.trace("thumb size: {}", Files.size(file)); } }
From source file:com.seleniumtests.it.driver.TestBrowserSnapshot.java
/** * Read the capture file to detect dimension * It assumes that picture background is white and counts the number of white pixels in width and height (first column and first row) * @return/*from w w w. j av a2 s .c om*/ * @throws IOException */ private Dimension getViewPortDimension(File picture) throws IOException { BufferedImage image = ImageIO.read(picture); int width = 0; int height = 0; for (width = 0; width < image.getWidth(); width++) { Color color = new Color(image.getRGB(width, 0)); if (!(color.equals(Color.WHITE) || color.equals(Color.YELLOW) || color.equals(Color.ORANGE) || color.equals(Color.GREEN) || color.equals(Color.RED))) { break; } } for (height = 0; height < image.getHeight(); height++) { Color color = new Color(image.getRGB(5, height)); if (!(color.equals(Color.WHITE) || color.equals(Color.YELLOW) || color.equals(Color.ORANGE) || color.equals(Color.GREEN) || color.equals(Color.RED))) { break; } } return new Dimension(width, height); }
From source file:com.kahlon.guard.controller.PersonImageManager.java
/** * Upload person image file//from w w w . j a v a 2 s . co m * * @param event * @throws java.io.IOException */ public void onUpload(FileUploadEvent event) throws IOException { photo = getRandomImageName(); byte[] data = IOUtils.toByteArray(event.getFile().getInputstream()); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String newFileName = servletContext.getRealPath("") + File.separator + "resources" + File.separator + "image" + File.separator + "temp" + File.separator + photo + ".png"; if (fileNames == null) { fileNames = new ArrayList<>(); } fileNames.add(newFileName); FileImageOutputStream imageOutput; try { imageOutput = new FileImageOutputStream(new File(newFileName)); imageOutput.write(data, 0, data.length); imageOutput.close(); BufferedImage originalImage = ImageIO.read(new File(newFileName)); int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType(); BufferedImage resizeImagePng = resizeImageWithHint(originalImage, type); ImageIO.write(resizeImagePng, "png", new File(newFileName)); } catch (Exception e) { throw new FacesException("Error in writing captured image."); } FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, msg); imageUpload = false; }