Example usage for java.awt.image BufferedImage TYPE_BYTE_BINARY

List of usage examples for java.awt.image BufferedImage TYPE_BYTE_BINARY

Introduction

In this page you can find the example usage for java.awt.image BufferedImage TYPE_BYTE_BINARY.

Prototype

int TYPE_BYTE_BINARY

To view the source code for java.awt.image BufferedImage TYPE_BYTE_BINARY.

Click Source Link

Document

Represents an opaque byte-packed 1, 2, or 4 bit image.

Usage

From source file:ConvertUtil.java

/**
 * Converts the source to 1-bit colour depth (monochrome). No transparency.
 * /* w ww  .  j  av  a2s.c o m*/
 * @param src
 *            the source image to convert
 * @return a copy of the source image with a 1-bit colour depth.
 */
public static BufferedImage convert1(BufferedImage src) {
    IndexColorModel icm = new IndexColorModel(1, 2, new byte[] { (byte) 0, (byte) 0xFF },
            new byte[] { (byte) 0, (byte) 0xFF }, new byte[] { (byte) 0, (byte) 0xFF });

    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);

    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);

    cco.filter(src, dest);

    return dest;
}

From source file:com.ackpdfbox.app.imageio.TIFFUtil.java

/**
 * Sets the ImageIO parameter compression type based on the given image.
 * @param image buffered image used to decide compression type
 * @param param ImageIO write parameter to update
 *//*w  w  w . j a v a 2s.c  o m*/
public static void setCompressionType(ImageWriteParam param, BufferedImage image) {
    // avoid error: first compression type is RLE, not optimal and incorrect for color images
    // TODO expose this choice to the user?
    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        param.setCompressionType("CCITT T.6");
    } else {
        param.setCompressionType("LZW");
    }
}

From source file:image.writer.ImageWriterExample2.java

private BufferedImage createAnImage(String compression, int pageNum) {
    boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only

    BufferedImage bimg;/*from   w  ww.  j a va2  s  . com*/
    if (monochrome) {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY);
    } else {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D g2d = bimg.createGraphics();
    g2d.setBackground(Color.white);
    g2d.clearRect(0, 0, 400, 200);
    g2d.setColor(Color.black);

    //Paint something
    paintSome(g2d, pageNum);

    return bimg;
}

From source file:com.org.barcode.Barcode.java

/**
 * To create bar code image //from w ww  .j  av a 2  s .c o  m
 * @param barcodeUrl
 * @return 
 */
public String getBarcode(String barcodeUrl) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
    Date date = Calendar.getInstance().getTime();
    String barcodeName = sdf.format(date) + ".jpg";
    try {
        //Create the barcode bean   
        Code39Bean bean = new Code39Bean();

        final int dpi = 50;
        //Configure the barcode generator   
        bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); //makes the narrow bar    
                                                         //width exactly one pixel   
        bean.setWideFactor(3);
        bean.doQuietZone(false);

        //Open output file   
        System.out.println("url1= " + barcodeUrl);
        File outputFile = new File(barcodeUrl + barcodeName);
        OutputStream out = new FileOutputStream(outputFile);
        try {
            //Set up the canvas provider for monochrome JPEG output    
            BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, "image/jpeg", dpi,
                    BufferedImage.TYPE_BYTE_BINARY, false, 0);

            //Generate the barcode   
            bean.generateBarcode(canvas, barcodeName);

            //Signal end of generation   
            canvas.finish();
        } finally {
            out.close();
        }
    } catch (Exception e) {
    }
    return barcodeName;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 4-bit colour using the given colour map. No
 * transparency.//from   w ww  .j av a2s  . c om
 * 
 * @param src
 *            the source image to convert
 * @param cmap
 *            the colour map, which should contain no more than 16 entries
 *            The entries are in the form RRGGBB (hex).
 * @return a copy of the source image with a 4-bit colour depth, with the
 *         custom colour pallette
 */
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
    IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);

    return dest;
}

From source file:com.aegon.pdf2tiff.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags.
 * {@see https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/
 *       apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java}
 * {@see http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/
 *       plugins/tiff/package-summary.html}
 * {@see http://partners.adobe.com/public/developer/tiff/index.html}
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata/*ww w . j  a v a  2  s.co m*/
 * @param dpi image dots per inch
 */
public static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", "com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet");
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    try {
        metadata.mergeTree(SUN_TIFF_FORMAT, root);
    } catch (IIOInvalidTreeException e) {
        // should never happen
        throw new RuntimeException(e);
    }

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}

From source file:image.writer.ImageWriterExample1.java

/**
 * Creates a bitmap file. We paint a few things on a bitmap and then save the bitmap using
 * an ImageWriter.//  ww  w  .  j ava2s.c  o m
 * @param outputFile the target file
 * @param format the target format (a MIME type, ex. "image/png")
 * @throws IOException In case of an I/O error
 */
public void generateBitmapUsingJava2D(File outputFile, String format) throws IOException {
    //String compression = "CCITT T.6";
    String compression = "PackBits";
    boolean monochrome = compression.startsWith("CCITT"); //CCITT is for 1bit b/w only

    BufferedImage bimg;
    if (monochrome) {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_BYTE_BINARY);
    } else {
        bimg = new BufferedImage(400, 200, BufferedImage.TYPE_INT_RGB);
    }

    Graphics2D g2d = bimg.createGraphics();
    g2d.setBackground(Color.white);
    g2d.clearRect(0, 0, 400, 200);
    g2d.setColor(Color.black);

    //Paint something
    paintSome(g2d, 1);

    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    try {

        ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor(format);
        ImageWriterParams params = new ImageWriterParams();
        params.setCompressionMethod(compression);
        params.setResolution(72);
        writer.writeImage(bimg, out, params);

    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:com.ackpdfbox.app.imageio.TIFFUtil.java

/**
 * Updates the given ImageIO metadata with Sun's custom TIFF tags, as described in
 * the <a href="https://svn.apache.org/repos/asf/xmlgraphics/commons/tags/commons-1_3_1/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOTIFFImageWriter.java">org.apache.xmlgraphics.image.writer.ImageIOTIFFImageWriter
 * sources</a>, /*w  ww . j  av a 2  s  .  co  m*/
 * the <a href="http://download.java.net/media/jai-imageio/javadoc/1.0_01/com/sun/media/imageio/plugins/tiff/package-summary.html">com.sun.media.imageio.plugins.tiff
 * package javadoc</a>
 * and the <a href="http://partners.adobe.com/public/developer/tiff/index.html">TIFF
 * specification</a>.
 *
 * @param image buffered image which will be written
 * @param metadata ImageIO metadata
 * @param dpi image dots per inch
 * @throws IIOInvalidTreeException if something goes wrong
 */
static void updateMetadata(IIOMetadata metadata, BufferedImage image, int dpi) throws IIOInvalidTreeException {
    debugLogMetadata(metadata, SUN_TIFF_FORMAT);

    if (!SUN_TIFF_FORMAT.equals(metadata.getNativeMetadataFormatName())) {
        LOG.debug("Using unknown TIFF image writer: " + metadata.getNativeMetadataFormatName());
        return;
    }

    IIOMetadataNode root = new IIOMetadataNode(SUN_TIFF_FORMAT);
    IIOMetadataNode ifd;
    if (root.getElementsByTagName("TIFFIFD").getLength() == 0) {
        ifd = new IIOMetadataNode("TIFFIFD");
        ifd.setAttribute("tagSets", tagSetClassName);
        root.appendChild(ifd);
    } else {
        ifd = (IIOMetadataNode) root.getElementsByTagName("TIFFIFD").item(0);
    }

    // standard metadata does not work, so we set the DPI manually
    ifd.appendChild(createRationalField(282, "XResolution", dpi, 1));
    ifd.appendChild(createRationalField(283, "YResolution", dpi, 1));
    ifd.appendChild(createShortField(296, "ResolutionUnit", 2)); // Inch

    ifd.appendChild(createLongField(278, "RowsPerStrip", image.getHeight()));
    ifd.appendChild(createAsciiField(305, "Software", "PDFBOX"));

    if (image.getType() == BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() == 1) {
        // set PhotometricInterpretation WhiteIsZero
        // because of bug in Windows XP preview
        ifd.appendChild(createShortField(262, "PhotometricInterpretation", 0));
    }

    metadata.mergeTree(SUN_TIFF_FORMAT, root);

    debugLogMetadata(metadata, SUN_TIFF_FORMAT);
}

From source file:com.sketchy.utils.image.SketchyImage.java

public static void save(SketchyImage sketchyImage, File file) throws Exception {
    if (!file.getParentFile().canWrite()) {
        throw new Exception("Can not write to File: " + file.getPath() + "!");
    }/*from  ww  w  . ja v a 2  s  .  c  o  m*/

    if (!StringUtils.endsWithIgnoreCase(file.getName(), ".png")) {
        throw new Exception("Can not save SketchyImage! Must be a .png file!");
    }

    Iterator<ImageWriter> imageWriters = ImageIO.getImageWritersByFormatName("png");
    ImageWriter imageWriter = null;
    if (imageWriters.hasNext()) { // Just get first one
        imageWriter = imageWriters.next();
    }
    if (imageWriter == null) {
        // this should never happen!! if so.. we got problems
        throw new Exception("Can not find ImageReader for .png Files!");
    }

    ImageOutputStream os = null;
    try {
        os = ImageIO.createImageOutputStream(file);
        imageWriter.setOutput(os);

        ImageWriteParam imageWriterParam = imageWriter.getDefaultWriteParam();
        IIOMetadata metadata = imageWriter.getDefaultImageMetadata(
                ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_BYTE_BINARY),
                imageWriterParam);

        String metaDataFormatName = metadata.getNativeMetadataFormatName();
        IIOMetadataNode metaDataNode = (IIOMetadataNode) metadata.getAsTree(metaDataFormatName);

        NodeList childNodes = metaDataNode.getElementsByTagName("pHYs");
        IIOMetadataNode physNode = null;
        if (childNodes.getLength() == 0) {
            physNode = new IIOMetadataNode("pHYs");
            physNode.setAttribute("pixelsPerUnitXAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
            physNode.setAttribute("pixelsPerUnitYAxis",
                    Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
            physNode.setAttribute("unitSpecifier", "meter"); // always meter
            metaDataNode.appendChild(physNode);
        } else {
            for (int nodeIdx = 0; nodeIdx < childNodes.getLength(); nodeIdx++) {
                physNode = (IIOMetadataNode) childNodes.item(nodeIdx);
                physNode.setAttribute("pixelsPerUnitXAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterWidth * 1000)));
                physNode.setAttribute("pixelsPerUnitYAxis",
                        Integer.toString((int) Math.ceil(sketchyImage.dotsPerMillimeterHeight * 1000)));
                physNode.setAttribute("unitSpecifier", "meter"); // always meter
                metaDataNode.appendChild(physNode);
            }
        }
        metadata.setFromTree(metaDataFormatName, metaDataNode);
        imageWriter.write(new IIOImage(sketchyImage.image, null, metadata));
        os.flush();
    } catch (Exception e) {
        throw new Exception("Error Saving SketchyImage File: " + file.getPath() + "!  " + e.getMessage());
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:app.gui.Histogram.java

private void MakeChart() {
    if (this.image.getType() != BufferedImage.TYPE_BYTE_BINARY) {
        XYDataset xydataset = createDataset();
        chart = createChart(xydataset);//from   w w  w.  j  a v a 2 s. co m
    } else {
        this.remove(this.jPanel1);
        DefaultCategoryDataset dataset = createbinaryDataset();
        chart = createChart(dataset);
    }
    saveHistogramToFile(chart);
    ChartPanel chartpanelTmp = new ChartPanel(chart);
    chartpanelTmp.setPreferredSize(new Dimension(200, 200));
    if (this.chartpanel != null && this.jPanel2.getComponents().length > 0) {
        this.jPanel2.remove(this.chartpanel);
    }
    this.chartpanel = chartpanelTmp;
    this.jPanel2.add(this.chartpanel);
    this.jPanel2.revalidate();
}