当前位置: 首页>>代码示例>>Java>>正文


Java ImageIO.createImageInputStream方法代码示例

本文整理汇总了Java中javax.imageio.ImageIO.createImageInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ImageIO.createImageInputStream方法的具体用法?Java ImageIO.createImageInputStream怎么用?Java ImageIO.createImageInputStream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.imageio.ImageIO的用法示例。


在下文中一共展示了ImageIO.createImageInputStream方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: createTestImage

import javax.imageio.ImageIO; //导入方法依赖的package包/类
private static ImageInputStream createTestImage(int type) throws IOException  {
    int w = 100;
    int h = 100;

    BufferedImage img = new BufferedImage(w, h, type);

    int dx = w / colors.length;

    for (int i = 0; i < colors.length; i++) {
        for (int x = i *dx; (x < (i + 1) * dx) && (x < w) ; x++) {
            for (int y = 0; y < h; y++) {
                img.setRGB(x, y, colors[i].getRGB());
            }
        }
    }

    File pwd = new File(".");
    File out = File.createTempFile("rgba_", ".png", pwd);
    System.out.println("Create file: " + out.getAbsolutePath());
    ImageIO.write(img, "PNG", out);
    return ImageIO.createImageInputStream(out);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:PngOutputTypeTest.java

示例2: cut

import javax.imageio.ImageIO; //导入方法依赖的package包/类
public static BufferedImage cut(String format, InputStream src, int x, int y, int w, int h) throws IOException {
	Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(format);
	ImageReader reader = (ImageReader) iterator.next();
	ImageInputStream iis = ImageIO.createImageInputStream(src);
	reader.setInput(iis, true);
	ImageReadParam param = reader.getDefaultReadParam();
	int imgWidth = reader.getWidth(0);
	int imgHeight = reader.getHeight(0);
	if (x + w > imgWidth) {
		w = imgWidth - x;
	}
	if (y + h > imgHeight) {
		h = imgHeight - y;
	}
	Rectangle rect = new Rectangle(x, y, w, h);
	param.setSourceRegion(rect);
	BufferedImage bi = reader.read(0, param);
	return bi;
}
 
开发者ID:szsucok,项目名称:sucok-framework,代码行数:20,代码来源:ImageSizeUtils.java

示例3: doTest

import javax.imageio.ImageIO; //导入方法依赖的package包/类
public void doTest(String compression, int bi_type) throws IOException {
    System.out.println("Test " + compression + " on " + getImageTypeName(bi_type));
    BufferedImage src = createTestImage(bi_type);
    writer.reset();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageOutputStream ios =
            ImageIO.createImageOutputStream(baos);
    writer.setOutput(ios);

    ImageWriteParam wparam = prepareWriteParam(compression);
    writer.write(null, new IIOImage(src, null, null), wparam);
    ios.flush();
    ios.close();

    // read result
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    ImageInputStream iis = ImageIO.createImageInputStream(bais);
    reader.reset();
    reader.setInput(iis);

    BufferedImage dst = reader.read(0);

    checkResult(dst);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:EmbeddedFormatTest.java

示例4: main

import javax.imageio.ImageIO; //导入方法依赖的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");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:JpegMetadataColorSpaceTest.java

示例5: doTest

import javax.imageio.ImageIO; //导入方法依赖的package包/类
public void doTest(ImageReaderSpi spi) throws IOException {
    System.out.println("Test for " + title +
            (canDecode ? " (can decode)" : " (can't decode)"));
    System.out.print("As a stream...");
    ImageInputStream iis =
            ImageIO.createImageInputStream(getDataStream());

    if (spi.canDecodeInput(iis) != canDecode) {
        throw new RuntimeException("Test failed: wrong decideion " +
                "for stream data");
    }
    System.out.println("OK");

    System.out.print("As a file...");
    iis = ImageIO.createImageInputStream(getDataFile());
    if (spi.canDecodeInput(iis) != canDecode) {
        throw new RuntimeException("Test failed: wrong decideion " +
                "for file data");
    }
    System.out.println("OK");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:CanDecodeTest.java

示例6: canDecodeInput

import javax.imageio.ImageIO; //导入方法依赖的package包/类
@Override
public boolean canDecodeInput(Object input) throws IOException {
  if (!(input instanceof ImageInputStream)) {
    input = ImageIO.createImageInputStream(input);
  }
  if (input == null) {
    return false;
  }
  ImageInputStream stream = (ImageInputStream)input;
  byte[] b = new byte[12];
  try {
    stream.mark();
    stream.readFully(b);
  } catch (IOException e) {
    return false;
  }
  return Arrays.equals(b, HEADER_MAGIC);
}
 
开发者ID:dbmdz,项目名称:imageio-jnr,代码行数:19,代码来源:OpenJp2ImageReaderSpi.java

示例7: test

import javax.imageio.ImageIO; //导入方法依赖的package包/类
private static void test(ImageReader reader, String format)
    throws IOException {
    boolean gotISE = false;
    File f = null;
    ImageInputStream stream = null;
    try {
        f = File.createTempFile("imageio", ".tmp");
        stream = ImageIO.createImageInputStream(f);
        reader.setInput(stream, true);

        try {
            int numImages = reader.getNumImages(true);
        } catch (IOException ioe) {
            gotISE = false;
        } catch (IllegalStateException ise) {
            gotISE = true;
        }
    } finally {
        if (stream != null) {
            stream.close();
        }

        reader.dispose();

        if (f != null) {
            Files.delete(f.toPath());
        }
    }

    if (!gotISE) {
        throw new RuntimeException("Failed to get desired exception for " +
                                   format + " reader!");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:35,代码来源:AllowSearch.java

示例8: TIFF

import javax.imageio.ImageIO; //导入方法依赖的package包/类
/**
    * 
    * @param imageFile
    * @param band form files with multiple band
    */
public TIFF(File imageFile,int band) {
   	this.imageFile=imageFile;
       try {
           Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("tiff");
           boolean worked=false;
           while(readers.hasNext()&&!worked){
           	Object obj=readers.next();
           	if( obj instanceof TIFFImageReader){
           		reader = (TIFFImageReader)obj;
           		iis=ImageIO.createImageInputStream(imageFile);
           		reader.setInput(iis);
           		try{
           			xSize=reader.getWidth(band);
           			ySize=reader.getHeight(band);
           			bounds=new Rectangle(0,0,xSize,ySize);
           		}catch(Exception e){
           			bounds=null;
           			logger.warn("Problem reading size information");
           		}	
           		worked=true;
           	}else{
           		
           	}
           }
           if(!worked){
           	logger.warn("No reader avalaible for this image");
           }
       } catch (Exception ex) {
       	logger.error(ex.getMessage(),ex);
       }	
}
 
开发者ID:ec-europa,项目名称:sumo,代码行数:37,代码来源:TIFF.java

示例9: read

import javax.imageio.ImageIO; //导入方法依赖的package包/类
public static BufferedImage read(String format, InputStream in) throws IOException {
	Iterator<ImageReader> iterator = ImageIO.getImageReadersByFormatName(format);
	ImageReader reader = (ImageReader) iterator.next();
	ImageInputStream iis = ImageIO.createImageInputStream(in);
	reader.setInput(iis, true);
	ImageReadParam param = reader.getDefaultReadParam();
	BufferedImage bi = reader.read(0, param);
	return bi;
}
 
开发者ID:szsucok,项目名称:sucok-framework,代码行数:10,代码来源:ImageUtils.java

示例10: getImageFormatName

import javax.imageio.ImageIO; //导入方法依赖的package包/类
private static String getImageFormatName(final byte[] imageBytes)
		throws IOException {
	ByteArrayInputStream input = new ByteArrayInputStream(imageBytes);
	ImageInputStream imageInput = ImageIO.createImageInputStream(input);
	Iterator<ImageReader> iterator = ImageIO.getImageReaders(imageInput);
	String imageFormatName = null;
	if (iterator.hasNext()) {
		ImageReader reader = iterator.next();
		imageFormatName = reader.getFormatName().toUpperCase();
	}

	try {
		return imageFormatName;
	} finally {
		if (imageInput != null) {
			imageInput.close();
		}
	}
}
 
开发者ID:marlonwang,项目名称:raven,代码行数:20,代码来源:ImageUtils.java

示例11: getPngFrames

import javax.imageio.ImageIO; //导入方法依赖的package包/类
/**
 * @param object File or InputStream
 */
public static List<InputStream> getPngFrames(Object object) {
    try {
        ImageInputStream imageInputStream = ImageIO.createImageInputStream(object);
        return getPngFrames(imageInputStream);
    } catch (IOException e) {
        e.printStackTrace();
        return new ArrayList<>();
    }
}
 
开发者ID:dmitrykolesnikovich,项目名称:featurea,代码行数:13,代码来源:GifFramesExtractor.java

示例12: createShortStream

import javax.imageio.ImageIO; //导入方法依赖的package包/类
private static ImageInputStream createShortStream() {
    try {
        byte[] integerTestArray = new byte[] { 80 };
        ByteArrayInputStream bais = new ByteArrayInputStream(integerTestArray);

        return ImageIO.createImageInputStream(bais);
    } catch (IOException e) {
        return null;
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:11,代码来源:ShortStreamTest.java

示例13: doTest

import javax.imageio.ImageIO; //导入方法依赖的package包/类
private static void doTest(int type) throws IOException {
    BufferedImage src = createTestImage(type);

    File f = new File("test.jpg");

    if (!ImageIO.write(src, "jpg", f)) {
        throw new RuntimeException("Failed to write test image.");
    }

    ImageInputStream iis = ImageIO.createImageInputStream(f);
    ImageReader reader = ImageIO.getImageReaders(iis).next();
    reader.setInput(iis);

    Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0);
    ImageTypeSpecifier srgb = null;
    ImageTypeSpecifier gray = null;
    // look for gray and srgb types
    while ((srgb == null || gray == null) && types.hasNext()) {
        ImageTypeSpecifier t = types.next();
        if (t.getColorModel().getColorSpace().getType() == TYPE_GRAY) {
            gray = t;
        }
        if (t.getColorModel().getColorSpace() == sRGB) {
            srgb = t;
        }
    }
    if (gray == null) {
        throw new RuntimeException("No gray type available.");
    }
    if (srgb == null) {
        throw new RuntimeException("No srgb type available.");
    }

    System.out.println("Read as GRAY...");
    testType(reader, gray, src);

    System.out.println("Read as sRGB...");
    testType(reader, srgb, src);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:40,代码来源:ReadAsGrayTest.java

示例14: main

import javax.imageio.ImageIO; //导入方法依赖的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");
                }
            }
        }
}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:71,代码来源:BMPPixelSpacingTest.java

示例15: connectorIconShouldHaveCorrectContentType

import javax.imageio.ImageIO; //导入方法依赖的package包/类
@Test
public void connectorIconShouldHaveCorrectContentType() throws IOException {
    try (MockWebServer mockWebServer = new MockWebServer(); final Buffer resultBuffer = new Buffer()) {
        mockWebServer.start();

        resultBuffer.writeAll(Okio.source(getClass().getResourceAsStream("test-image.png")));

        mockWebServer.enqueue(new MockResponse().setHeader(CONTENT_TYPE, "image/png").setBody(resultBuffer));

        final Connector connector = new Connector.Builder().id("connector-id").icon(mockWebServer.url("/u/23079786").toString())
            .build();
        when(dataManager.fetch(Connector.class, "connector-id")).thenReturn(connector);
        when(dataManager.fetchAll(Integration.class)).thenReturn(ListResult.of(Collections.emptyList()));

        final Response response = handler.getConnectorIcon("connector-id").get();

        assertThat(response.getStatusInfo()).isEqualTo(Response.Status.OK).as("Wrong status code");
        assertThat(response.getHeaderString(CONTENT_TYPE)).isEqualTo("image/png").as("Wrong content type");
        assertThat(response.getHeaderString(CONTENT_LENGTH)).isEqualTo("2018").as("Wrong content length");

        final StreamingOutput so = (StreamingOutput) response.getEntity();
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try (BufferedSink sink = Okio.buffer(Okio.sink(bos)); BufferedSource source = new Buffer();
            ImageInputStream iis = ImageIO.createImageInputStream(source.inputStream());) {
            so.write(sink.outputStream());
            source.readAll(sink);
            final Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
            if (readers.hasNext()) {
                final ImageReader reader = readers.next();
                try {
                    reader.setInput(iis);
                    final Dimension dimensions = new Dimension(reader.getWidth(0), reader.getHeight(0));
                    assertThat(dimensions.getHeight()).isEqualTo(106d).as("Wrong image height");
                    assertThat(dimensions.getWidth()).isEqualTo(106d).as("Wrong image width");
                } finally {
                    reader.dispose();
                }
            }
        }
    }
}
 
开发者ID:syndesisio,项目名称:syndesis,代码行数:42,代码来源:ConnectorHandlerTest.java


注:本文中的javax.imageio.ImageIO.createImageInputStream方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。