use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class CompressionVals method main.
public static void main(String[] args) {
ImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float[] vals = iwp.getCompressionQualityValues();
String[] descs = iwp.getCompressionQualityDescriptions();
if (vals.length != (descs.length + 1)) {
throw new RuntimeException("Test failed: Values array is not " + "one larger than descriptions array");
}
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class BooleanAttributes method test.
public static void test(String mimeType, boolean useStreamMeta, String metaXml, String... boolXpaths) throws Exception {
BufferedImage img = new BufferedImage(16, 16, BufferedImage.TYPE_INT_RGB);
ImageWriter iw = ImageIO.getImageWritersByMIMEType(mimeType).next();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
iw.setOutput(ios);
ImageWriteParam param = null;
IIOMetadata streamMeta = iw.getDefaultStreamMetadata(param);
IIOMetadata imageMeta = iw.getDefaultImageMetadata(new ImageTypeSpecifier(img), param);
IIOMetadata meta = useStreamMeta ? streamMeta : imageMeta;
Source src = new StreamSource(new StringReader(metaXml));
DOMResult dst = new DOMResult();
transform(src, dst);
Document doc = (Document) dst.getNode();
Element node = doc.getDocumentElement();
String metaFormat = node.getNodeName();
// Verify that the default metadata gets formatted correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, false);
meta.mergeTree(metaFormat, node);
// Verify that the merged metadata gets formatte correctly.
verify(meta.getAsTree(metaFormat), boolXpaths, true);
iw.write(streamMeta, new IIOImage(img, null, imageMeta), param);
iw.dispose();
ios.close();
ImageReader ir = ImageIO.getImageReader(iw);
byte[] bytes = os.toByteArray();
if (bytes.length == 0)
throw new AssertionError("Zero length image file");
ByteArrayInputStream is = new ByteArrayInputStream(bytes);
ImageInputStream iis = new MemoryCacheImageInputStream(is);
ir.setInput(iis);
if (useStreamMeta)
meta = ir.getStreamMetadata();
else
meta = ir.getImageMetadata(0);
// Verify again after writing and re-reading the image
verify(meta.getAsTree(metaFormat), boolXpaths, true);
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class BMPPluginTest method main.
public static void main(String[] args) {
if (args.length > 0) {
format = args[0];
System.out.println("Test format " + format);
}
init();
System.out.println("IR=" + ir);
System.out.println("IW=" + iw);
ImageIO.setUseCache(false);
for (int i = 0; i < types.length; i++) {
boolean bPassed = true;
Object reason = null;
try {
BufferedImage image = createTestImage(types[i]);
ImageWriteParam param = iw.getDefaultWriteParam();
BMPPluginTest t = new BMPPluginTest(image, param);
boolean res = false;
res = t.test();
if (!res) {
bPassed = false;
reason = new String("Null result");
}
} catch (IllegalArgumentException ex) {
System.out.println("Expected exception type was caught: " + ex);
} catch (Throwable ex) {
System.out.println("FAILED");
ex.printStackTrace();
bPassed = false;
reason = ex;
throw new RuntimeException("Test for type " + types[i] + " FAILED due to exception");
}
/*
System.out.println("Type " + types[i] + " result: " +
(bPassed ? "PASSED" : "FAILED") +
((reason != null) ? (" Reason: " + reason) : ""));
*/
System.out.println("Test for type " + types[i] + " PASSED");
}
System.out.println("END OF TEST");
}
use of javax.imageio.ImageWriteParam in project jdk8u_jdk by JetBrains.
the class ImageWriteParamMisc method test4434886.
private static void test4434886() {
ImageWriteParam iwp = new ImageWriteParam4434886();
iwp.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
try {
iwp.setTiling(-1, -2, -3, -4);
throw new RuntimeException("Failed to get IAE!");
} catch (IllegalArgumentException e) {
}
}
use of javax.imageio.ImageWriteParam in project screenbird by adamhub.
the class ImageUtil method markAndCompress.
/**
* Adds a watermark to the image and compresses it.
* @param imageSource
* @param compressionQuality
* @param mark
* @param markImageSource
*/
public static void markAndCompress(String imageSource, float compressionQuality, boolean mark, String markImageSource) {
try {
File fileImageSource = new File(imageSource);
if (!fileImageSource.exists()) {
throw new ImageDoesNotExistException("Mark Image doesn't exists: " + fileImageSource.getAbsolutePath());
}
BufferedImage bufferedImage = ImageIO.read(fileImageSource);
if (mark) {
addMark(bufferedImage, markImageSource, 1.0f, ImageUtil.MARK_LEFT_BOTTOM);
}
// Get a jpeg writer
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("jpg");
if (iter.hasNext()) {
writer = (ImageWriter) iter.next();
}
if (writer == null) {
throw new IOException("Could not get JPEG writer");
}
// Prepare output file
ImageOutputStream ios = ImageIO.createImageOutputStream(fileImageSource);
if (ios == null) {
throw new IOException("Could not open image stream to write image watermark");
}
writer.setOutput(ios);
// Set the compression quality
ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
if (compressionQuality < 0.0F || compressionQuality > 1.0F) {
compressionQuality = 1.0F;
}
iwparam.setCompressionQuality(compressionQuality);
// Write the image
writer.write(null, new IIOImage(bufferedImage, null, null), iwparam);
// Cleanup
ios.flush();
writer.dispose();
ios.close();
} catch (IllegalArgumentException e) {
log(e);
} catch (IOException e) {
log(e);
} catch (ImageDoesNotExistException e) {
log(e);
}
}
Aggregations