List of usage examples for javax.imageio ImageTypeSpecifier getBufferedImageType
public int getBufferedImageType()
From source file:org.apache.carbondata.sdk.file.ImageTest.java
public void binaryToCarbonWithHWD(String sourceImageFolder, String outputPath, String preDestPath, String sufAnnotation, final String sufImage, int numToWrite) throws Exception { int num = 1;/*from w w w .j a va 2s . c om*/ Field[] fields = new Field[7]; fields[0] = new Field("height", DataTypes.INT); fields[1] = new Field("width", DataTypes.INT); fields[2] = new Field("depth", DataTypes.INT); fields[3] = new Field("binaryName", DataTypes.STRING); fields[4] = new Field("binary", DataTypes.BINARY); fields[5] = new Field("labelName", DataTypes.STRING); fields[6] = new Field("labelContent", DataTypes.STRING); byte[] originBinary = null; // read and write image data for (int j = 0; j < num; j++) { Object[] files = listFiles(sourceImageFolder, sufImage).toArray(); int index = 0; if (null != files) { CarbonWriter writer = CarbonWriter.builder().outputPath(outputPath).withCsvInput(new Schema(fields)) .withBlockSize(256).writtenBy("SDKS3Example").withPageSizeInMb(1).build(); for (int i = 0; i < files.length; i++) { if (0 == index % numToWrite) { writer.close(); writer = CarbonWriter.builder().outputPath(outputPath).withCsvInput(new Schema(fields)) .withBlockSize(256).writtenBy("SDKS3Example").withPageSizeInMb(1).build(); } index++; // read image and encode to Hex File file = new File((String) files[i]); System.out.println(file.getCanonicalPath()); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); int depth = 0; boolean isGray; boolean hasAlpha; BufferedImage bufferedImage = null; try { bufferedImage = ImageIO.read(file); isGray = bufferedImage.getColorModel().getColorSpace().getType() == ColorSpace.TYPE_GRAY; hasAlpha = bufferedImage.getColorModel().hasAlpha(); if (isGray) { depth = 1; } else if (hasAlpha) { depth = 4; } else { depth = 3; } } catch (Exception e) { e.printStackTrace(); System.out.println(i); ImageInputStream stream = new FileImageInputStream(new File(file.getCanonicalPath())); Iterator<ImageReader> iter = ImageIO.getImageReaders(stream); Exception lastException = null; while (iter.hasNext()) { ImageReader reader = null; try { reader = (ImageReader) iter.next(); ImageReadParam param = reader.getDefaultReadParam(); reader.setInput(stream, true, true); Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0); while (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); System.out .println(imageTypeSpecifier.getColorModel().getColorSpace().getType()); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); break; } } bufferedImage = reader.read(0, param); isGray = bufferedImage.getColorModel().getColorSpace() .getType() == ColorSpace.TYPE_GRAY; hasAlpha = bufferedImage.getColorModel().hasAlpha(); if (isGray) { depth = 1; } else if (hasAlpha) { depth = 4; } else { depth = 3; } if (null != bufferedImage) break; } catch (Exception e2) { lastException = e2; } finally { if (null != reader) reader.dispose(); } } // If you don't have an image at the end of all readers if (null == bufferedImage) { if (null != lastException) { throw lastException; } } } finally { originBinary = new byte[bis.available()]; while ((bis.read(originBinary)) != -1) { } String txtFileName = file.getCanonicalPath().split(sufImage)[0] + sufAnnotation; BufferedInputStream txtBis = new BufferedInputStream(new FileInputStream(txtFileName)); String txtValue = null; byte[] txtBinary = null; txtBinary = new byte[txtBis.available()]; while ((txtBis.read(txtBinary)) != -1) { txtValue = new String(txtBinary, "UTF-8"); } // write data writer.write(new Object[] { bufferedImage.getHeight(), bufferedImage.getWidth(), depth, file.getCanonicalPath(), originBinary, txtFileName, txtValue.replace("\n", "") }); bis.close(); } } writer.close(); } } CarbonReader reader = CarbonReader.builder(outputPath).build(); System.out.println("\nData:"); int i = 0; while (i < 20 && reader.hasNext()) { Object[] row = (Object[]) reader.readNextRow(); byte[] outputBinary = (byte[]) row[1]; System.out.println(row[2] + " " + row[3] + " " + row[4] + " " + row[5] + " image size:" + outputBinary.length + " " + row[0]); // save image, user can compare the save image and original image String destString = preDestPath + i + sufImage; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString)); bos.write(outputBinary); bos.close(); i++; } System.out.println("\nFinished"); reader.close(); }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
private static SizeAndBufferedImage calcScaledSize(ImageInputStream stream, String suffix, int maxWidth, int maxHeight, boolean fill) { Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try {/*www. j a va 2s . c om*/ reader.setInput(stream, true, true); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); Size size = new Size(width, height, false); Size scaledSize = computeScaledSize(width, height, maxWidth, maxHeight, fill); SizeAndBufferedImage all = new SizeAndBufferedImage(size, scaledSize); int readerMinIndex = reader.getMinIndex(); ImageReadParam param = reader.getDefaultReadParam(); Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0); while (imageTypes.hasNext()) { try { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); } double memoryKB = (width * height * 4) / 1024d; if (memoryKB > 2000) {// check limit at 20MB double free = Runtime.getRuntime().freeMemory() / 1024d; if (free > memoryKB) { all.setImage(reader.read(readerMinIndex, param)); } else { // make sub sampling to save memory int ratio = (int) Math.round(Math.sqrt(memoryKB / free)); param.setSourceSubsampling(ratio, ratio, 0, 0); all.setImage(reader.read(readerMinIndex, param)); } } else { all.setImage(reader.read(readerMinIndex, param)); } return all; } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); } } } catch (IOException e) { log.error(e.getMessage(), e); } finally { reader.dispose(); } } else { log.error("No reader found for given format: " + suffix, null); } return null; }
From source file:org.olat.core.commons.services.image.spi.ImageHelperImpl.java
private static SizeAndBufferedImage getImage(ImageInputStream stream, String suffix) { Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); if (iter.hasNext()) { ImageReader reader = iter.next(); try {//from ww w . j a v a 2s . c o m reader.setInput(stream, true, true); int width = reader.getWidth(reader.getMinIndex()); int height = reader.getHeight(reader.getMinIndex()); Size size = new Size(width, height, false); SizeAndBufferedImage all = new SizeAndBufferedImage(size, null); int readerMinIndex = reader.getMinIndex(); ImageReadParam param = reader.getDefaultReadParam(); Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0); while (imageTypes.hasNext()) { try { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); } all.setImage(reader.read(readerMinIndex, param)); return all; } catch (IllegalArgumentException e) { log.warn(e.getMessage(), e); } } } catch (IOException e) { log.error(e.getMessage(), e); } finally { reader.dispose(); } } else { log.error("No reader found for given format: " + suffix, null); } return null; }
From source file:sernet.verinice.service.commands.LoadAttachmentFile.java
private BufferedImage readImageFromByteArrayFallback() throws IOException { ImageInputStream in = ImageIO .createImageInputStream(new ByteArrayInputStream(getAttachmentFile().getFileData())); Iterator<ImageReader> iter = ImageIO.getImageReaders(in); BufferedImage image = null;/*from w ww .j av a 2 s. c o m*/ while (iter.hasNext()) { ImageReader reader = null; try { reader = (ImageReader) iter.next(); ImageReadParam param = reader.getDefaultReadParam(); reader.setInput(in, true, true); Iterator<ImageTypeSpecifier> imageTypes = reader.getImageTypes(0); while (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); break; } } image = reader.read(0, param); if (null != image) { break; } } catch (Exception e) { getLog().error("Error while reading image the advanced way. DbId: " + getAttachmentFile().getDbId(), e); } finally { if (null != reader) { reader.dispose(); } } } return image; }