Java Code Examples for javax.imageio.ImageTypeSpecifier#createFromBufferedImageType()

The following examples show how to use javax.imageio.ImageTypeSpecifier#createFromBufferedImageType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageTypeSpecifierBitsPerBand.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) {
    int biType = BufferedImage.TYPE_USHORT_565_RGB;
    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(biType);

    int b0 = type.getBitsPerBand(0);
    int b1 = type.getBitsPerBand(1);
    int b2 = type.getBitsPerBand(2);

    if (b0 != 5 || b1 != 6 || b2 != 5) {
        throw new RuntimeException("Got incorrect bits per band value!");
    }

    boolean gotIAE = false;
    try {
        int b3 = type.getBitsPerBand(3);
    } catch (IllegalArgumentException e) {
        gotIAE = true;
    }
    if (!gotIAE) {
        throw new RuntimeException
            ("Failed to get IllegalArgumentException for band == 3!");
    }
}
 
Example 2
Source File: PNGCanvas.java    From SwissQRBill with MIT License 5 votes vote down vote up
/**
 * Saves image as PDF and stores meta data to indicate the resolution.
 */
private static void createPNG(BufferedImage image, OutputStream os, int resolution) throws IOException {

    ImageWriter writer = null;
    ImageWriteParam writeParam = null;
    IIOMetadata metadata = null;

    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName("png"); iw.hasNext(); ) {
        writer = iw.next();
        writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(image.getType());
        metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported())
            break;
    }

    if (writer == null || writeParam == null)
        throw new QRBillGenerationException("No valid PNG writer found");

    addDpiMetadata(metadata, resolution);
    addTextMetadata(metadata);

    try (ImageOutputStream stream = ImageIO.createImageOutputStream(os)) {
        writer.setOutput(stream);
        writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
    }
}
 
Example 3
Source File: MergeTreeTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ImageWriter iw =
        (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    ImageOutputStream ios =
        ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);

    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);

    boolean isFailed = false;

    String[] fmts = meta.getMetadataFormatNames();
    for (int i=0; i<fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
 
Example 4
Source File: DestTypeTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public ImageWriteParam getWriteParam() {
    ImageWriteParam p =  w.getDefaultWriteParam();
    p.setSourceBands(new int[] {0, 1, 2});
    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
    p.setDestinationType(type);

    return p;
}
 
Example 5
Source File: MergeTreeTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    ImageWriter iw =
        (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();

    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);

    ImageOutputStream ios =
        ImageIO.createImageOutputStream(new File("MergeTreeTest.jpeg"));
    iw.setOutput(ios);

    IIOMetadata meta = iw.getDefaultImageMetadata(type, null);

    boolean isFailed = false;

    String[] fmts = meta.getMetadataFormatNames();
    for (int i=0; i<fmts.length; i++) {
        System.out.print("Format: " + fmts[i] + " ... ");
        Node root = meta.getAsTree(fmts[i]);
        try {
            meta.mergeTree(fmts[i], root);
        } catch (NullPointerException e) {
            throw new RuntimeException("Test failed for format " + fmts[i], e);
        }
        System.out.println("PASSED");
    }
}
 
Example 6
Source File: BaseImageIoTest.java    From java-image-processing-survival-guide with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBufferedImage(BufferedImage bufferedImage, float quality, int dpi, String formatName, File targetFile) throws Exception {

    System.out.println("Saving " + targetFile.getPath());

    if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
        JPEGImageWriter imageWriter = (JPEGImageWriter) ImageIO.getImageWritersBySuffix(formatName).next();
        ImageWriteParam writeParam = imageWriter.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(typeSpecifier, writeParam);

        if (formatName.equalsIgnoreCase("jpg") || formatName.equalsIgnoreCase("jpeg")) {
            Element tree = (Element) metadata.getAsTree("javax_imageio_jpeg_image_1.0");
            Element jfif = (Element) tree.getElementsByTagName("app0JFIF").item(0);
            jfif.setAttribute("Xdensity", Integer.toString(dpi));
            jfif.setAttribute("Ydensity", Integer.toString(dpi));
            jfif.setAttribute("resUnits", "1");
            metadata.setFromTree("javax_imageio_jpeg_image_1.0", tree);
        }

        if (quality >= 0 && quality <= 1f) {
            JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) imageWriter.getDefaultWriteParam();
            jpegParams.setCompressionMode(JPEGImageWriteParam.MODE_EXPLICIT);
            jpegParams.setCompressionQuality(quality);
        }

        FileOutputStream os = new FileOutputStream(targetFile);
        final ImageOutputStream stream = ImageIO.createImageOutputStream(os);

        try {
            imageWriter.setOutput(stream);
            imageWriter.write(metadata, new IIOImage(bufferedImage, null, metadata), writeParam);
        } finally {
            stream.close();
        }
    } else {
        writeBufferedImage(bufferedImage, formatName, targetFile);
    }
}
 
Example 7
Source File: DefaultDrawerImageUtils.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static ImageAndResolution convertToInputStreamJPG(BufferedImage buffImage, int dpi) throws IOException {
	ImageWriter writer = getImageWriter("jpeg");

	JPEGImageWriteParam jpegParams = (JPEGImageWriteParam) writer.getDefaultWriteParam();
	jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
	jpegParams.setCompressionQuality(1);

	ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
	IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, jpegParams);

	initDpiJPG(metadata, dpi);

	return getImageAndResolution(buffImage, dpi, writer, jpegParams, metadata);
}
 
Example 8
Source File: JPEGImageReader.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 9
Source File: JPEGImageReader.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 10
Source File: JPEGImageReader.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 11
Source File: GifSequenceWriter.java    From open-ig with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Creates a new GifSequenceWriter.
 * 
 * @param outputStream
 *            the ImageOutputStream to be written to
 * @param imageType
 *            one of the imageTypes specified in BufferedImage
 * @param timeBetweenFramesMS
 *            the time between frames in miliseconds
 * @param loopContinuously
 *            wether the gif should loop repeatedly
 * @throws IOException if the underlying stream throws it or if no gif ImageWriters are found
 */
public GifSequenceWriter(ImageOutputStream outputStream, int imageType,
		int timeBetweenFramesMS, boolean loopContinuously)
		throws IOException {
	// my method to create a writer
	gifWriter = getWriter();
	imageWriteParam = gifWriter.getDefaultWriteParam();
	ImageTypeSpecifier imageTypeSpecifier = ImageTypeSpecifier
			.createFromBufferedImageType(imageType);

	imageMetaData = gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
			imageWriteParam);

	String metaFormatName = imageMetaData.getNativeMetadataFormatName();

	IIOMetadataNode root = (IIOMetadataNode) imageMetaData
			.getAsTree(metaFormatName);

	IIOMetadataNode graphicsControlExtensionNode = getNode(root,
			"GraphicControlExtension");

	graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
	graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
	graphicsControlExtensionNode.setAttribute("transparentColorFlag",
			"FALSE");
	graphicsControlExtensionNode.setAttribute("delayTime", Integer
			.toString(timeBetweenFramesMS / 10));
	graphicsControlExtensionNode.setAttribute("transparentColorIndex", "0");

	IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
	commentsNode.setAttribute("CommentExtension", "Created by MAH");

	IIOMetadataNode appEntensionsNode = getNode(root,
			"ApplicationExtensions");

	IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");

	child.setAttribute("applicationID", "NETSCAPE");
	child.setAttribute("authenticationCode", "2.0");

	int loop = loopContinuously ? 0 : 1;

	child.setUserObject(new byte[] { 0x1, (byte) (loop & 0xFF),
			(byte) ((loop >> 8) & 0xFF) });
	appEntensionsNode.appendChild(child);

	imageMetaData.setFromTree(metaFormatName, root);

	gifWriter.setOutput(outputStream);

	gifWriter.prepareWriteSequence(null);
}
 
Example 12
Source File: JPEGImageReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 13
Source File: JPEGImageReader.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 14
Source File: JPEGImageReader.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 15
Source File: GifSequenceWriter.java    From graphicsfuzz with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new GifSequenceWriter.
 *
 * @param outputStream the ImageOutputStream to be written to
 * @param imageType one of the imageTypes specified in BufferedImage
 * @param timeBetweenFramesMs the time between frames in miliseconds
 * @param loopContinuously wether the gif should loop repeatedly
 * @throws IIOException if no gif ImageWriters are found
 *
 * @author Elliot Kroo (elliot[at]kroo[dot]net)
 */
public GifSequenceWriter(
    ImageOutputStream outputStream,
    int imageType,
    int timeBetweenFramesMs,
    boolean loopContinuously) throws IIOException, IOException {
  // my method to create a writer
  gifWriter = getWriter();
  imageWriteParam = gifWriter.getDefaultWriteParam();
  ImageTypeSpecifier imageTypeSpecifier =
      ImageTypeSpecifier.createFromBufferedImageType(imageType);

  imageMetaData =
      gifWriter.getDefaultImageMetadata(imageTypeSpecifier,
          imageWriteParam);

  String metaFormatName = imageMetaData.getNativeMetadataFormatName();

  IIOMetadataNode root = (IIOMetadataNode)
      imageMetaData.getAsTree(metaFormatName);

  IIOMetadataNode graphicsControlExtensionNode = getNode(
      root,
      "GraphicControlExtension");

  graphicsControlExtensionNode.setAttribute("disposalMethod", "none");
  graphicsControlExtensionNode.setAttribute("userInputFlag", "FALSE");
  graphicsControlExtensionNode.setAttribute(
      "transparentColorFlag",
      "FALSE");
  graphicsControlExtensionNode.setAttribute(
      "delayTime",
      Integer.toString(timeBetweenFramesMs / 10));
  graphicsControlExtensionNode.setAttribute(
      "transparentColorIndex",
      "0");

  IIOMetadataNode commentsNode = getNode(root, "CommentExtensions");
  commentsNode.setAttribute("CommentExtension", "Created by MAH");

  IIOMetadataNode child = new IIOMetadataNode("ApplicationExtension");

  child.setAttribute("applicationID", "NETSCAPE");
  child.setAttribute("authenticationCode", "2.0");

  IIOMetadataNode appEntensionsNode = getNode(
      root,
      "ApplicationExtensions");

  int loop = loopContinuously ? 0 : 1;

  child.setUserObject(new byte[]{ 0x1, (byte) (loop & 0xFF), (byte)
      ((loop >> 8) & 0xFF)});
  appEntensionsNode.appendChild(child);

  imageMetaData.setFromTree(metaFormatName, root);

  gifWriter.setOutput(outputStream);

  gifWriter.prepareWriteSequence(null);
}
 
Example 16
Source File: JPEGImageReader.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 17
Source File: JPEGImageReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
protected ImageTypeSpecifier produce() {
    switch (csCode) {
        case JPEG.JCS_GRAYSCALE:
            return ImageTypeSpecifier.createFromBufferedImageType
                    (BufferedImage.TYPE_BYTE_GRAY);
        case JPEG.JCS_RGB:
            return ImageTypeSpecifier.createInterleaved(JPEG.JCS.sRGB,
                    JPEG.bOffsRGB,
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
        case JPEG.JCS_RGBA:
            return ImageTypeSpecifier.createPacked(JPEG.JCS.sRGB,
                    0xff000000,
                    0x00ff0000,
                    0x0000ff00,
                    0x000000ff,
                    DataBuffer.TYPE_INT,
                    false);
        case JPEG.JCS_YCC:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[2],
                    DataBuffer.TYPE_BYTE,
                    false,
                    false);
            } else {
                return null;
            }
        case JPEG.JCS_YCCA:
            if (JPEG.JCS.getYCC() != null) {
                return ImageTypeSpecifier.createInterleaved(
                        JPEG.JCS.getYCC(),
                    JPEG.bandOffsets[3],
                    DataBuffer.TYPE_BYTE,
                    true,
                    false);
            } else {
                return null;
            }
        default:
            return null;
    }
}
 
Example 18
Source File: MapLayout.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Export to a picture file
 *
 * @param fileName File path
 * @param dpi DPI
 * @throws java.io.FileNotFoundException
 * @throws javax.print.PrintException
 */
public void exportToPicture(String fileName, Integer dpi) throws FileNotFoundException, PrintException, IOException {
    if (dpi == null) {
        exportToPicture(fileName);
    } else {
        File output = new File(fileName);
        output.delete();

        int width = _pageBounds.width;
        int height = _pageBounds.height;
        String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
        if (formatName.equals("jpg")) {
            formatName = "jpeg";
            saveImage_Jpeg(fileName, width, height, dpi);
            return;
        }

        double scaleFactor = dpi / 72.0;
        BufferedImage image = new BufferedImage((int)(width * scaleFactor), (int)(height * scaleFactor), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        AffineTransform at = g.getTransform();
        at.scale(scaleFactor, scaleFactor);
        g.setTransform(at);
        paintGraphics(g);
        for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
            ImageWriter writer = iw.next();
            ImageWriteParam writeParam = writer.getDefaultWriteParam();
            ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
            IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
            if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
                continue;
            }

            ImageUtil.setDPI(metadata, dpi);

            final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
            try {
                writer.setOutput(stream);
                writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
            } finally {
                stream.close();
            }
            break;
        }
        g.dispose();
    }
}
 
Example 19
Source File: ChartPanel.java    From MeteoInfo with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Save image
 *
 * @param fileName File name
 * @param dpi DPI
 * @param width Width
 * @param height Height
 * @param sleep Sleep seconds for web map layer
 * @throws IOException
 * @throws java.lang.InterruptedException
 */
public void saveImage(String fileName, int dpi, int width, int height, Integer sleep) throws IOException, InterruptedException {
    File output = new File(fileName);
    output.delete();

    String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);
    if (formatName.equals("jpg")) {
        formatName = "jpeg";
        saveImage_Jpeg(fileName, width, height, dpi);
        return;
    }

    double scaleFactor = dpi / 72.0;
    BufferedImage image = new BufferedImage((int)(width * scaleFactor), (int)(height * scaleFactor), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = image.createGraphics();
    AffineTransform at = g.getTransform();
    at.scale(scaleFactor, scaleFactor);
    g.setTransform(at);
    paintGraphics(g, width, height);
    for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
        ImageWriter writer = iw.next();
        ImageWriteParam writeParam = writer.getDefaultWriteParam();
        ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
        IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
        if (metadata == null) {
            metadata = writer.getDefaultImageMetadata(typeSpecifier, null);
        }
        if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
            continue;
        }

        ImageUtil.setDPI(metadata, dpi);

        if (sleep != null) {
            Thread.sleep(sleep * 1000);
        }
        final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
        try {
            writer.setOutput(stream);
            writer.write(metadata, new IIOImage(image, null, metadata), writeParam);
        } finally {
            stream.close();
        }
        break;
    }
    g.dispose();
}
 
Example 20
Source File: DefaultDrawerImageUtils.java    From dss with GNU Lesser General Public License v2.1 3 votes vote down vote up
private static ImageAndResolution convertToInputStreamPNG(BufferedImage buffImage, int dpi) throws IOException {
	ImageWriter writer = getImageWriter("png");

	ImageWriteParam imageWriterParams = writer.getDefaultWriteParam();

	ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_ARGB);
	IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, imageWriterParams);

	initDpiPNG(metadata, dpi);

	return getImageAndResolution(buffImage, dpi, writer, imageWriterParams, metadata);
}