Java Code Examples for javax.imageio.stream.MemoryCacheImageOutputStream#close()

The following examples show how to use javax.imageio.stream.MemoryCacheImageOutputStream#close() . 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: CommUtils.java    From tools-ocr with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static byte[] imageToBytes(BufferedImage img) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream outputStream = new MemoryCacheImageOutputStream(byteArrayOutputStream);
    try {
        Iterator iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter) iter.next();
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(IMAGE_QUALITY);
        writer.setOutput(outputStream);
        IIOImage image = new IIOImage(img, null, null);
        writer.write(null, image, iwp);
        writer.dispose();
        byte[] result = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        outputStream.close();
        return result;
    } catch (IOException e) {
        StaticLog.error(e);
        return new byte[0];
    }
}
 
Example 2
Source File: ByteArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final byte BYTE_VALUE = -10;
    final int NUM_BITS = 8;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeByte(BYTE_VALUE);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(BYTE_VALUE, array.elementAt(0));
}
 
Example 3
Source File: ShortArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeShort(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example 4
Source File: Float16ArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testConstructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeFloat(1f);
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    float16Array = new Float16Array(in, 1);
    assertEquals(1, float16Array.length());
    /*
     * Compare with 1.875 because of the float writing instead of float16.
     */
    assertEquals(1.875f, float16Array.elementAt(0), 0);
}
 
Example 5
Source File: UnsignedByteArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final int HIGH_NIBBLE = 1;
    final int LOW_NIBBLE = 2;

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeByte((HIGH_NIBBLE << 4) | LOW_NIBBLE);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 2, 4);

    assertEquals(2, array.length());
    assertEquals(HIGH_NIBBLE, array.elementAt(0));
    assertEquals(LOW_NIBBLE, array.elementAt(1));
}
 
Example 6
Source File: LongArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeLong(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example 7
Source File: IntArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void constructorRead() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeInt(18);
    os.close();
    baos.close();

    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());

    final ArrayWrapper array = factory.create(in, 1, NUM_BITS);

    assertEquals(1, array.length());
    assertEquals(18, array.elementAt(0));
}
 
Example 8
Source File: Float64ArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testConstructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeDouble(1.0);
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    float64Array = new Float64Array(in, 1);
    assertEquals(1, float64Array.length());
    assertEquals(1.0, float64Array.elementAt(0), 0);
}
 
Example 9
Source File: StringArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testConstructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeUTF("");
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    stringArray = new StringArray(in, 1);
    assertEquals(1, stringArray.length());
    assertEquals("", stringArray.elementAt(0));
}
 
Example 10
Source File: BoolArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void constructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeBoolean(false);
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    boolArray = new BoolArray(in, 1);
    assertEquals(1, boolArray.length());
    assertFalse(boolArray.elementAt(0));
}
 
Example 11
Source File: Float32ArrayTest.java    From zserio with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testConstructor() throws IOException
{
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final MemoryCacheImageOutputStream os = new MemoryCacheImageOutputStream(baos);
    os.writeFloat(1f);
    os.close();
    baos.close();
    final ByteArrayBitStreamReader in = new ByteArrayBitStreamReader(baos.toByteArray());
    float32Array = new Float32Array(in, 1);
    assertEquals(1, float32Array.length());
    assertEquals(1.0f, float32Array.elementAt(0), 0);
}
 
Example 12
Source File: CCITTFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a new CCITT group 4 (T6) compressed image XObject from a b/w BufferedImage. This
 * compression technique usually results in smaller images than those produced by {@link LosslessFactory#createFromImage(PDDocument, BufferedImage)
 * }.
 *
 * @param document the document to create the image as part of.
 * @param image the image.
 * @return a new image XObject.
 * @throws IOException if there is an error creating the image.
 * @throws IllegalArgumentException if the BufferedImage is not a b/w image.
 */
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image)
        throws IOException
{
    if (image.getType() != BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() != 1)
    {
        throw new IllegalArgumentException("Only 1-bit b/w images supported");
    }
    
    int height = image.getHeight();
    int width = image.getWidth();

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos);

    for (int y = 0; y < height; ++y)
    {
        for (int x = 0; x < width; ++x)
        {
            // flip bit to avoid having to set /BlackIs1
            mcios.writeBits(~(image.getRGB(x, y) & 1), 1);
        }
        if (mcios.getBitOffset() != 0)
        {
            mcios.writeBits(0, 8 - mcios.getBitOffset());
        }
    }
    mcios.flush();
    mcios.close();

    return prepareImageXObject(document, bos.toByteArray(), width, height, PDDeviceGray.INSTANCE);
}
 
Example 13
Source File: LosslessFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static PDImageXObject createFromGrayImage(BufferedImage image, PDDocument document)
        throws IOException
{
    int height = image.getHeight();
    int width = image.getWidth();
    int[] rgbLineBuffer = new int[width];
    int bpc = image.getColorModel().getPixelSize();
    ByteArrayOutputStream baos = new ByteArrayOutputStream(((width*bpc/8)+(width*bpc%8 != 0 ? 1:0))*height);
    MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(baos);
    for (int y = 0; y < height; ++y)
    {
        for (int pixel : image.getRGB(0, y, width, 1, rgbLineBuffer, 0, width))
        {
            mcios.writeBits(pixel & 0xFF, bpc);
        }

        int bitOffset = mcios.getBitOffset();
        if (bitOffset != 0)
        {
            mcios.writeBits(0, 8 - bitOffset);
        }
    }
    mcios.flush();
    mcios.close();
    return prepareImageXObject(document, baos.toByteArray(),
            image.getWidth(), image.getHeight(), bpc, PDDeviceGray.INSTANCE);
}
 
Example 14
Source File: JpegWriter.java    From scrimage with Apache License 2.0 5 votes vote down vote up
@Override
   public void write(AwtImage image, ImageMetadata metadata, OutputStream out) throws IOException {

      javax.imageio.ImageWriter writer = ImageIO.getImageWritersByFormatName("jpeg").next();
      ImageWriteParam params = writer.getDefaultWriteParam();
      if (compression < 100) {
         params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
         params.setCompressionQuality(compression / 100f);
      }
      if (progressive) {
         params.setProgressiveMode(ImageWriteParam.MODE_DEFAULT);
      } else {
         params.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
      }

      // in openjdk, awt cannot write out jpegs that have a transparency bit, even if that is set to 255.
      // see http://stackoverflow.com/questions/464825/converting-transparent-gif-png-to-jpeg-using-java
      // so have to convert to a non alpha type
      BufferedImage noAlpha;
      if (image.awt().getColorModel().hasAlpha()) {
         noAlpha = image.toImmutableImage().removeTransparency(java.awt.Color.WHITE).toNewBufferedImage(BufferedImage.TYPE_INT_RGB);
      } else {
         noAlpha = image.awt();
      }

      MemoryCacheImageOutputStream output = new MemoryCacheImageOutputStream(out);
      writer.setOutput(output);
      writer.write(null, new IIOImage(noAlpha, null, null), params);
      output.close();
      writer.dispose();
//        IOUtils.closeQuietly(out);
   }
 
Example 15
Source File: LZWFilter.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void encode(InputStream rawData, OutputStream encoded, COSDictionary parameters)
        throws IOException
{
    List<byte[]> codeTable = createCodeTable();
    int chunk = 9;

    byte[] inputPattern = null;
    final MemoryCacheImageOutputStream out = new MemoryCacheImageOutputStream(encoded);
    out.writeBits(CLEAR_TABLE, chunk);
    int foundCode = -1;
    int r;
    while ((r = rawData.read()) != -1)
    {
        byte by = (byte) r;
        if (inputPattern == null)
        {
            inputPattern = new byte[] { by };
            foundCode = by & 0xff;
        }
        else
        {
            inputPattern = Arrays.copyOf(inputPattern, inputPattern.length + 1);
            inputPattern[inputPattern.length - 1] = by;
            int newFoundCode = findPatternCode(codeTable, inputPattern);
            if (newFoundCode == -1)
            {
                // use previous
                chunk = calculateChunk(codeTable.size() - 1, 1);
                out.writeBits(foundCode, chunk);
                // create new table entry
                codeTable.add(inputPattern);

                if (codeTable.size() == 4096)
                {
                    // code table is full
                    out.writeBits(CLEAR_TABLE, chunk);
                    codeTable = createCodeTable();
                }

                inputPattern = new byte[] { by };
                foundCode = by & 0xff;
            }
            else
            {
                foundCode = newFoundCode;
            }
        }
    }
    if (foundCode != -1)
    {
        chunk = calculateChunk(codeTable.size() - 1, 1);
        out.writeBits(foundCode, chunk);
    }

    // PPDFBOX-1977: the decoder wouldn't know that the encoder would output 
    // an EOD as code, so he would have increased his own code table and 
    // possibly adjusted the chunk. Therefore, the encoder must behave as 
    // if the code table had just grown and thus it must be checked it is
    // needed to adjust the chunk, based on an increased table size parameter
    chunk = calculateChunk(codeTable.size(), 1);

    out.writeBits(EOD, chunk);
    
    // pad with 0
    out.writeBits(0, 7);
    
    // must do or file will be empty :-(
    out.flush();
    out.close();
}