use of javax.imageio.stream.MemoryCacheImageOutputStream in project jdk8u_jdk by JetBrains.
the class MemoryCacheImageOutputStreamTest method main.
public static void main(String[] args) throws IOException {
try {
MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
// or write anything, for that matter
stream.write(0);
stream.flush();
} catch (Exception e) {
throw new RuntimeException("Error flushing stream: " + e);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
byte[] b = new byte[30 * 256];
byte byteVal = (byte) 0;
for (int i = 0; i < b.length; i++) {
b[i] = byteVal++;
}
// Write 261,120 bytes
for (int i = 0; i < 34; i++) {
ios.write(b);
}
// Scatter 256 values at positions 1000, 2000, ...
// Using both write(int) and write(byte[])
byte[] buf = new byte[1];
for (int i = 0; i < 256; i += 2) {
ios.seek(1000 * i);
ios.write(i);
ios.seek(1000 * (i + 1));
buf[0] = (byte) (i + 1);
ios.write(buf);
}
// Re-read scattered values
for (int i = 0; i < 256; i++) {
ios.seek(1000 * i);
int val = ios.read();
if (val != i) {
System.out.println("Got bad value (1) at pos = " + (1000 * i));
}
}
// Discard two buffers and re-read scattered values
ios.flushBefore(2 * 8192);
for (int i = 0; i < 256; i++) {
long pos = 1000 * i;
if (pos >= 2 * 8192) {
ios.seek(pos);
int val = ios.read();
if (val != i) {
System.out.println("Got bad value (2) at pos = " + (1000 * i));
}
}
}
ios.close();
byte[] data = os.toByteArray();
for (int i = 0; i < data.length; i++) {
byte val = data[i];
if ((i < 256000) && (i % 1000) == 0) {
if (val != (byte) (i / 1000)) {
System.out.println("Got bad value (3) at pos = " + i);
}
} else {
byte gval = (byte) ((i % (30 * 256)) % 256);
if (val != gval) {
System.out.println("Got bad value (4) at pos = " + i + "(got " + (val & 0xff) + " wanted " + (gval & 0xff) + ")");
}
}
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project wcomponents by BorderTech.
the class ThumbnailUtil method createScaledJPEG.
/**
* This method creates an array of bytes representing a JPEG image that is a "scaled" version of the given
* {@link Image}.
*
* @param image The image to be turned into a scaled JPEG.
* @param scaledSize The size to which the given <em>image</em> is to be scaled.
* @return A byte[] representing the JPEG image containing the scaled {@link Image}.
* @throws IOException on any sort of error.
*/
private static byte[] createScaledJPEG(final Image image, final Dimension scaledSize) throws IOException {
// Scale the image.
Image scaledImage = image.getScaledInstance(scaledSize.width, scaledSize.height, Image.SCALE_SMOOTH);
// Create a BufferedImage copy of the scaledImage.
BufferedImage bufferedImage = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawImage(scaledImage, 0, 0, null);
scaledImage.flush();
graphics.dispose();
// Convert the scaled image to a JPEG byte array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MemoryCacheImageOutputStream mciis = new MemoryCacheImageOutputStream(baos);
ImageIO.write(bufferedImage, IMAGE_JPEG_FORMAT, mciis);
mciis.flush();
bufferedImage.flush();
byte[] jpeg = baos.toByteArray();
mciis.close();
return jpeg;
}
Aggregations