本文整理汇总了Java中javax.imageio.ImageReader.getImageMetadata方法的典型用法代码示例。如果您正苦于以下问题:Java ImageReader.getImageMetadata方法的具体用法?Java ImageReader.getImageMetadata怎么用?Java ImageReader.getImageMetadata使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageReader
的用法示例。
在下文中一共展示了ImageReader.getImageMetadata方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: runTest
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public void runTest(Object ctx, int numReps) {
final Context ictx = (Context)ctx;
final ImageReader reader = ictx.reader;
final boolean seekForwardOnly = ictx.seekForwardOnly;
final boolean ignoreMetadata = ictx.ignoreMetadata;
do {
try {
ImageInputStream iis = ictx.createImageInputStream();
reader.setInput(iis, seekForwardOnly, ignoreMetadata);
reader.getImageMetadata(0);
reader.reset();
iis.close();
ictx.closeOriginalStream();
} catch (IOException e) {
e.printStackTrace();
}
} while (--numReps >= 0);
}
示例2: main
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
// Generate some trivial image and save it to a temporary array
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),
"gif", tmp);
// Read the stream
ImageInputStream in = new MemoryCacheImageInputStream(
new ByteArrayInputStream(tmp.toByteArray()));
ImageReader reader = ImageIO.getImageReaders(in).next();
reader.setInput(in);
// Retrieve standard image metadata tree
IIOMetadata meta = reader.getImageMetadata(0);
if (meta == null || !meta.isStandardMetadataFormatSupported()) {
throw new Error("Test failure: Missing metadata");
}
Element root = (Element) meta.
getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
// Test getElementsByTagName("*")
if (root.getElementsByTagName("*").getLength() == 0) {
throw new RuntimeException("getElementsByTagName(\"*\") returns"
+ " nothing");
}
}
示例3: main
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
String fileName = "nomarkers.jpg";
String sep = System.getProperty("file.separator");
String dir = System.getProperty("test.src", ".");
String filePath = dir+sep+fileName;
System.out.println("Test file: " + filePath);
File file = new File(filePath);
ImageInputStream stream = ImageIO.createImageInputStream(file);
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
if(readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(stream);
IIOMetadata metadata = reader.getImageMetadata(0);
IIOMetadataNode standardTree = (IIOMetadataNode)
metadata.getAsTree
(IIOMetadataFormatImpl.standardMetadataFormatName);
IIOMetadataNode colorSpaceType = (IIOMetadataNode)
standardTree.getElementsByTagName("ColorSpaceType").item(0);
String colorSpaceName = colorSpaceType.getAttribute("name");
if(colorSpaceName.equals("RGB"))
throw new RuntimeException("Identified incorrect ColorSpace");
}
}
示例4: main
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException {
// Generate some trivial image and save it to a temporary array
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
ImageIO.write(new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB),
"gif", tmp);
// Read it back in
ImageInputStream in = new MemoryCacheImageInputStream(
new ByteArrayInputStream(tmp.toByteArray()));
ImageReader reader = ImageIO.getImageReaders(in).next();
reader.setInput(in);
// Retrieve standard image metadata tree
IIOMetadata meta = reader.getImageMetadata(0);
if (meta == null || !meta.isStandardMetadataFormatSupported()) {
throw new Error("Test failure: Missing metadata");
}
Element root = (Element) meta.
getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName);
NodeList nodeList = root.
getElementsByTagName(root.getFirstChild().getNodeName());
/*
* Accessing the nth node should return null and not throw
* IndexOutOfBoundsException.
*/
Node n = (nodeList.item(nodeList.getLength()));
}
示例5: main
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static void main(String args[]) throws Exception {
Iterator<ImageReader> readers = ImageIO.getImageReadersBySuffix("simp");
ImageReader simpReader = null;
if (readers.hasNext()) {
simpReader = readers.next();
System.out.println("reader="+simpReader);
}
if (simpReader == null) {
throw new RuntimeException("Reader not found.");
}
ImageReaderSpi spi = simpReader.getOriginatingProvider();
IIOMetadataFormat spiFmt =
spi.getImageMetadataFormat("simp_metadata_1.0");
System.out.println("fmt from SPI=" + spiFmt);
ByteArrayInputStream bais = new ByteArrayInputStream(simpData);
ImageInputStream iis = new MemoryCacheImageInputStream(bais);
simpReader.setInput(iis);
BufferedImage bi = simpReader.read(0);
System.out.println(bi);
IIOMetadata metadata = simpReader.getImageMetadata(0);
System.out.println("Image metadata="+metadata);
IIOMetadataFormat format =
metadata.getMetadataFormat("simp_metadata_1.0");
System.out.println("Image metadata format="+format);
if (format == null) {
throw new RuntimeException("MetadataFormat not found.");
}
}
示例6: initializeAnnotations
import javax.imageio.ImageReader; //导入方法依赖的package包/类
private void initializeAnnotations() {
try {
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);
Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
if (readers.hasNext()) {
ImageReader reader = readers.next();
reader.setInput(iis);
IIOMetadata imageMetadata = reader.getImageMetadata(0);
org.w3c.dom.Node root = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName());
NodeList childNodes = root.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
org.w3c.dom.Node item = childNodes.item(i);
if (item.getNodeName().equals("tEXt")) {
org.w3c.dom.Node textNode = item;
NodeList entryNodes = textNode.getChildNodes();
for (int j = 0; j < entryNodes.getLength(); j++) {
org.w3c.dom.Node entry = entryNodes.item(j);
if (entry.getNodeName().equals("tEXtEntry")) {
NamedNodeMap attributes = entry.getAttributes();
String kw = attributes.getNamedItem("keyword").getNodeValue();
String value = attributes.getNamedItem("value").getNodeValue();
Pattern p = Pattern.compile("a1810-(\\d+)-(\\d+\\.\\d+)-(\\d+\\.\\d+)-(\\d+\\.\\d+)-(\\d+\\.\\d+)");
Matcher matcher = p.matcher(kw);
if (matcher.matches()) {
Annotation annotation = new Annotation();
annotation.setX(Double.parseDouble(matcher.group(2)));
annotation.setY(Double.parseDouble(matcher.group(3)));
annotation.setWidth(Double.parseDouble(matcher.group(4)));
annotation.setHeight(Double.parseDouble(matcher.group(5)));
annotation.setText(value);
annotation.setFill(ANNOTATION_COLOR);
annotations.add(annotation);
}
}
}
}
}
reader.dispose();
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例7: test
import javax.imageio.ImageReader; //导入方法依赖的package包/类
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);
}
示例8: main
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
// Header contaning X & Y pixels-per-meter more than value 1
byte[] bmpHeaderData = { (byte) 0x42, (byte) 0x4d, (byte) 0x7e,
(byte) 0x06, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x3e, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x28, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x64, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x64,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x02,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
(byte) 0xff };
ImageInputStream imageInput = ImageIO.
createImageInputStream(new ByteArrayInputStream(bmpHeaderData));
for (Iterator<ImageReader> it = ImageIO.getImageReaders(imageInput);
it.hasNext(); ) {
ImageReader reader = it.next();
reader.setInput(imageInput);
IIOMetadata metadata = reader.getImageMetadata(0);
Node rootNode = metadata.getAsTree("javax_imageio_1.0");
NodeList nl = rootNode.getChildNodes();
//Parse until you get Dimension child node
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if ((node.getNodeName()).equals("Dimension")) {
//get childnode list under Dimension node
NodeList cl = node.getChildNodes();
//Corresponding node indices under Dimension node
int horizontalNodeIndex = 1;
int verticalNodeIndex = 2;
Node horizontalNode = cl.item(horizontalNodeIndex);
Node verticalNode = cl.item(verticalNodeIndex);
//get attributes for horizontal and vertical nodes
NamedNodeMap horizontalAttr = horizontalNode.
getAttributes();
NamedNodeMap verticalAttr = verticalNode.getAttributes();
//since they have only one attribute index is 0
int attributeIndex = 0;
Node horizontalValue = horizontalAttr.item(attributeIndex);
Node verticalValue = verticalAttr.item(attributeIndex);
float horizontalNodeValue = Float.
parseFloat((horizontalValue.getNodeValue()));
float verticalNodeValue = Float.
parseFloat((verticalValue.getNodeValue()));
float expectedHorizontalValue, expectedVerticalValue;
// in test metadata xPixelsPerMeter & yPixelsPerMeter is 2
expectedHorizontalValue = expectedVerticalValue =
1000.0F / 2;
//expected and returned values should be same
if ((Float.compare(horizontalNodeValue,
expectedHorizontalValue) != 0) ||
(Float.compare(verticalNodeValue,
expectedVerticalValue) != 0)) {
throw new RuntimeException("Invalid pixel spacing");
}
}
}
}
}
示例9: doTest
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public void doTest() {
try {
System.out.println(this.getDescription());
if (param.getCompressionMode() != ImageWriteParam.MODE_EXPLICIT) {
System.out.println("Warning: compression mode is not MODE_EXPLICIT");
}
// change metadata according to ImageWriteParam
IIOMetadata new_meta = iw.convertImageMetadata(meta, new ImageTypeSpecifier(img), param);
IIOImage iio_img = new IIOImage(img, null, new_meta);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
iw.setOutput(ios);
System.out.print("write image...");
System.out.println("Current compression Type is \""+param.getCompressionType()+"\"");
iw.write(new_meta, iio_img, param);
//iw.write(iio_img);
System.out.println("OK");
System.out.print("read image ... ");
ios.flush();
byte[] ba_image = baos.toByteArray();
System.out.println("Array length=" + ba_image.length);
FileOutputStream fos = new FileOutputStream(new File(param.getCompressionType()+".bmp"));
fos.write(ba_image);
fos.flush();
fos = null;
ByteArrayInputStream bais = new ByteArrayInputStream(ba_image);
ImageReader ir = ImageIO.getImageReader(iw);
ir.setInput(ImageIO.createImageInputStream(bais));
BufferedImage res = ir.read(0);
System.out.println("OK");
if (!param.getCompressionType().equals("BI_JPEG")) {
System.out.print("compare images ... ");
boolean r = compare(img,res);
System.out.println(r?"OK":"FAILED");
if (!r) {
throw new RuntimeException("Compared images are not equals. Test failed.");
}
}
BMPMetadata mdata = (BMPMetadata)ir.getImageMetadata(0);
if (!param.getCompressionType().equals(param.getCompressionTypes()[mdata.compression])) {
throw new RuntimeException("Different compression value");
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("Unexpected exception: " + ex);
}
}