List of usage examples for java.awt.image BufferedImage getType
public int getType()
From source file:kz.supershiny.core.services.ImageService.java
/** * Creates thumb for image.// w ww . j ava 2s . c o m * * @param originalData original image in byte array * @param type original - 0, large - 1, small - 2 * @return resized image in byte array */ public static byte[] resizeImage(byte[] originalData, ImageSize type) { //if original flag, then return original if (type.equals(ImageSize.ORIGINAL)) { return originalData; } BufferedImage originalImage = null; BufferedImage resizedImage = null; byte[] result = null; //convert bytes to BufferedImage try (InputStream in = new ByteArrayInputStream(originalData)) { originalImage = ImageIO.read(in); } catch (IOException ex) { LOG.error("Cannot convert byte array to BufferedImage!", ex); return null; } //get original size int scaledHeight = originalImage.getHeight(); int scaledWidth = originalImage.getWidth(); switch (type) { case LARGE: scaledWidth = LARGE_WIDTH; scaledHeight = LARGE_HEIGHT; break; case SMALL: scaledWidth = SMALL_WIDTH; scaledHeight = SMALL_HEIGHT; break; default: break; } //calculate aspect ratio float ratio = 1.0F * originalImage.getWidth() / originalImage.getHeight(); if (ratio > 1.0F) { scaledHeight = (int) (scaledHeight / ratio); } else { scaledWidth = (int) (scaledWidth * ratio); } //resize with hints resizedImage = new BufferedImage(scaledWidth, scaledHeight, originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType()); Graphics2D g = resizedImage.createGraphics(); g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null); g.dispose(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //convert BufferedImage to bytes try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(resizedImage, "png", baos); baos.flush(); result = baos.toByteArray(); } catch (IOException ex) { LOG.error("Cannot convert BufferedImage to byte array!", ex); } return result; }
From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java
/** * Calculate the SSIM between two files/*from w w w . ja va 2 s . c o m*/ * @param pOne first image to compare * @param pTwo second image to compare * @param pHeatMapFilename ssim heat map image filename (can be null) * @param pMin list for return value - ssim minimum (can be null) * @param pVariance list for return value - ssim variance (can be null) * @return calculated ssim */ public static double calcSSIM(final File pOne, final File pTwo, final String pHeatMapFilename, List<Double> pMin, List<Double> pVariance) { BufferedImage imageOne = null; try { imageOne = Imaging.getBufferedImage(pOne); } catch (IOException e) { printError(pOne, false, false, pTwo, false); return -1; } catch (NullPointerException e) { printError(pOne, false, false, pTwo, false); return -1; } catch (ImageReadException e) { printError(pOne, false, false, pTwo, false); return -1; } //getRGB only returns 8 bits per component, so what about 16-bit images? final int[] oneA = imageOne.getRGB(0, 0, imageOne.getWidth(), imageOne.getHeight(), null, 0, imageOne.getWidth()); final int width = imageOne.getWidth(); final int height = imageOne.getHeight(); final boolean greyscale = (imageOne.getType() == BufferedImage.TYPE_BYTE_GRAY || imageOne.getType() == BufferedImage.TYPE_USHORT_GRAY); imageOne = null; BufferedImage imageTwo = null; try { imageTwo = Imaging.getBufferedImage(pTwo); } catch (IOException e) { printError(pOne, true, true, pTwo, false); return -1; } catch (NullPointerException e) { printError(pOne, true, true, pTwo, false); return -1; } catch (ImageReadException e) { printError(pOne, true, true, pTwo, false); return -1; } //getRGB only returns 8 bits per component, so what about 16-bit images? final int[] twoA = imageTwo.getRGB(0, 0, imageTwo.getWidth(), imageTwo.getHeight(), null, 0, imageTwo.getWidth()); imageTwo = null; final double ssim = calcSSIM(oneA, twoA, width, height, greyscale, pHeatMapFilename, pMin, pVariance); return ssim; }
From source file:com.sketchy.utils.image.SketchyImage.java
public SketchyImage(BufferedImage image, double dotsPerMillimeterWidth, double dotsPerMillimeterHeight) { // make sure image is a byte array // if not, then convert it if (image.getType() != BufferedImage.TYPE_BYTE_INDEXED) { this.image = toByteImage(image); } else {//from ww w . ja v a 2s. c om this.image = image; } this.dotsPerMillimeterWidth = dotsPerMillimeterWidth; this.dotsPerMillimeterHeight = dotsPerMillimeterHeight; }
From source file:org.iish.visualmets.services.ImageTransformation.java
/** * Returns a 90 degrees rotated image/* w w w. jav a 2 s . c o m*/ * * @param bi image * @return a rotated image */ private BufferedImage RotateImage90Degrees(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage biFlip = new BufferedImage(height, width, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { biFlip.setRGB(height - 1 - j, i, bi.getRGB(i, j)); } return biFlip; }
From source file:org.iish.visualmets.services.ImageTransformation.java
/** * Returns a 180 degrees rotated image/*from w w w .j a v a 2s . co m*/ * * @param bi image * @return a rotated image */ private BufferedImage RotateImage180Degrees(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage biFlip = new BufferedImage(width, height, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { biFlip.setRGB(width - 1 - i, height - 1 - j, bi.getRGB(i, j)); } return biFlip; }
From source file:org.iish.visualmets.services.ImageTransformation.java
/** * Returns a 270 degrees rotated image//from www .j ava2s .co m * * @param bi image * @return a rotated image */ private BufferedImage RotateImage270Degrees(BufferedImage bi) { int width = bi.getWidth(); int height = bi.getHeight(); BufferedImage biFlip = new BufferedImage(height, width, bi.getType()); for (int i = 0; i < width; i++) for (int j = 0; j < height; j++) { biFlip.setRGB(j, width - 1 - i, bi.getRGB(i, j)); } return biFlip; }
From source file:de.thm.arsnova.ImageUtils.java
/** * Rescales an image represented by a Base64-encoded {@link String} * * @param originalImageString/*from ww w . j av a 2 s . c o m*/ * The original image represented by a Base64-encoded * {@link String} * @param width * the new width * @param height * the new height * @return The rescaled Image as Base64-encoded {@link String}, returns null * if the passed-on image isn't in a valid format (a Base64-Image). */ public String createCover(String originalImageString, final int width, final int height) { if (!isBase64EncodedImage(originalImageString)) { return null; } else { final String[] imgInfo = extractImageInfo(originalImageString); // imgInfo isn't null and contains two fields, this is checked by "isBase64EncodedImage"-Method final String extension = imgInfo[0]; final String base64String = imgInfo[1]; byte[] imageData = Base64.decodeBase64(base64String); try { BufferedImage originalImage = ImageIO.read(new ByteArrayInputStream(imageData)); BufferedImage newImage = new BufferedImage(width, height, originalImage.getType()); Graphics2D g = newImage.createGraphics(); final double ratio = ((double) originalImage.getWidth()) / ((double) originalImage.getHeight()); int x = 0, y = 0, w = width, h = height; if (originalImage.getWidth() > originalImage.getHeight()) { final int newWidth = (int) Math.round((float) height * ratio); x = -(newWidth - width) >> 1; w = newWidth; } else if (originalImage.getWidth() < originalImage.getHeight()) { final int newHeight = (int) Math.round((float) width / ratio); y = -(newHeight - height) >> 1; h = newHeight; } g.drawImage(originalImage, x, y, w, h, null); g.dispose(); StringBuilder result = new StringBuilder(); result.append("data:image/"); result.append(extension); result.append(";base64,"); ByteArrayOutputStream output = new ByteArrayOutputStream(); ImageIO.write(newImage, extension, output); output.flush(); output.close(); result.append(Base64.encodeBase64String(output.toByteArray())); return result.toString(); } catch (IOException e) { LOGGER.error(e.getLocalizedMessage()); return null; } } }
From source file:com.kahlon.guard.controller.PersonImageManager.java
/** * Upload person image file/*from w ww . jav a 2 s.c o 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; }
From source file:eu.novait.imageresizer.helpers.ImageConverter.java
public void process() throws IOException { BufferedImage inImage = ImageIO.read(this.file); double ratio = (double) inImage.getWidth() / (double) inImage.getHeight(); int w = this.width; int h = this.height; if (inImage.getWidth() >= inImage.getHeight()) { w = this.width; h = (int) Math.round(w / ratio); } else {/*from w w w . ja va 2 s. c om*/ h = this.height; w = (int) Math.round(ratio * h); } int type = inImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : inImage.getType(); BufferedImage outImage = new BufferedImage(w, h, type); Graphics2D g = outImage.createGraphics(); g.drawImage(inImage, 0, 0, w, h, null); g.dispose(); String ext = FilenameUtils.getExtension(this.file.getAbsolutePath()); String t = "jpg"; switch (ext) { case "png": t = "png"; break; } ImageIO.write(outImage, t, this.outputfile); }
From source file:org.geomajas.plugin.rasterizing.layer.RasterDirectLayer.java
private BufferedImage makeOpaque(BufferedImage image) { if (image.getType() == BufferedImage.TYPE_CUSTOM) { log.warn("makeOpaque {} Unknown Image Type 0: ", getTitle()); return image; }//from ww w . jav a2 s. co m BufferedImage opaqueCopy = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); Graphics2D g1 = opaqueCopy.createGraphics(); g1.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, getOpacity())); g1.drawImage(image, null, 0, 0); g1.dispose(); return opaqueCopy; }