本文整理汇总了Java中javax.imageio.ImageReader.getWidth方法的典型用法代码示例。如果您正苦于以下问题:Java ImageReader.getWidth方法的具体用法?Java ImageReader.getWidth怎么用?Java ImageReader.getWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.imageio.ImageReader
的用法示例。
在下文中一共展示了ImageReader.getWidth方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getPictureSize
import javax.imageio.ImageReader; //导入方法依赖的package包/类
/**
* Extracts the picture size of a given Image.
*
* @param path Path to the image
* @return Image-size in pixel
* @throws IOException Throws exception when file-access fails
*/
public static Dimension getPictureSize(final String path) throws IOException {
try (ImageInputStream in = ImageIO.createImageInputStream(path)) {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(in);
return new Dimension(reader.getWidth(0), reader.getHeight(0));
} finally {
reader.dispose();
}
}
return null;
}
}
示例2: run
import javax.imageio.ImageReader; //导入方法依赖的package包/类
/**
* Gets the size of an image.
*
* @param name the image name
* @param in the input stream
* @return the size of the image
*
* @throws BrokenImageException if the image is faulty
* @throws UnrecognizedImageTypeException if the image type is unknown
* @throws IOException if reading the image goes wrong
*/
public Dimension run(String name, InputStream in) throws IOException {
final ImageInputStream stream = new MemoryCacheImageInputStream(in);
final Iterator<ImageReader> i = ImageIO.getImageReaders(stream);
if (!i.hasNext()) throw new UnrecognizedImageTypeException(name);
final ImageReader reader = i.next();
try {
reader.setInput(stream);
return new Dimension(reader.getWidth(0), reader.getHeight(0));
}
finally {
reader.dispose();
}
}
示例3: cut
import javax.imageio.ImageReader; //导入方法依赖的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;
}
示例4: getImageDimensions
import javax.imageio.ImageReader; //导入方法依赖的package包/类
/**
* getImageDimensions.
*
* @param resourceFile File
* @return Dimension
*/
static Dimension getImageDimensions(final File resourceFile) {
registerAllServicesProviders();
try (ImageInputStream in = createImageInputStream(resourceFile)) {
final Iterator<ImageReader> readers = getImageReaders(in);
if (readers.hasNext()) {
final ImageReader reader = readers.next();
try {
reader.setInput(in);
return new Dimension(reader.getWidth(0), reader.getHeight(0));
} finally {
reader.dispose();
}
}
} catch (final IOException e) {
e.printStackTrace();
}
return null;
}
示例5: getSize
import javax.imageio.ImageReader; //导入方法依赖的package包/类
/**
* 获取图片的尺寸
*
* @param extName
* 后缀名
* @param imgContent
* 图片内容
* @return 数组:[宽,高]
*/
public int[] getSize(String extName, byte[] imgContent) {
if (imgContent == null || WakaUtils.string.isEmpty(extName)) {
throw new ZhhrUtilException("获取文件大小失败!入参为空");
}
// 字节转图片流
try (ImageInputStream imgInputStream = byteArrayStreamToImageInputStream(imgContent)) {
// 取得图片读取器
ImageReader imgReader = getImageReader(extName);
// 把文件流加载到图片读取器中
imgReader.setInput(imgInputStream, true);
return new int[] { imgReader.getWidth(0), imgReader.getHeight(0) };
} catch (IOException e) {
throw new ZhhrUtilException(e.getMessage(), e);
}
}
示例6: testUpdateIcon
import javax.imageio.ImageReader; //导入方法依赖的package包/类
@Test
public void testUpdateIcon() throws IOException {
final LinkedMultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
multipartData.add("icon", new InputStreamResource(getClass().getResourceAsStream("test-image.png")));
final ResponseEntity<Connector> updated = post("/api/v1/connectors/twitter/icon",
multipartData, Connector.class, tokenRule.validToken(), HttpStatus.OK,
multipartHeaders());
assertThat(updated.getBody().getId()).isPresent();
assertThat(updated.getBody().getIcon()).isNotBlank().startsWith("db:");
final ResponseEntity<ByteArrayResource> got = get("/api/v1/connectors/twitter/icon", ByteArrayResource.class);
assertThat(got.getHeaders().getFirst("Content-Type")).isEqualTo("image/png");
try (ImageInputStream iis = ImageIO.createImageInputStream(got.getBody().getInputStream());) {
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();
}
}
}
}
示例7: connectorIconShouldHaveCorrectContentType
import javax.imageio.ImageReader; //导入方法依赖的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();
}
}
}
}
}
示例8: testUpdateConnectorAndIconViaMultipart
import javax.imageio.ImageReader; //导入方法依赖的package包/类
@Test
public void testUpdateConnectorAndIconViaMultipart() throws IOException {
final Connector initialConnector = dataManager.fetch(Connector.class, "twitter");
assertThat(initialConnector.getIcon()).isNotBlank().doesNotStartWith("db:");
final Connector connectorWithNewdescription = new Connector.Builder().createFrom(initialConnector)
.description("Updated!").build();
final LinkedMultiValueMap<String, Object> multipartData = new LinkedMultiValueMap<>();
multipartData.add("connector", connectorWithNewdescription);
multipartData.add("icon", new InputStreamResource(getClass().getResourceAsStream("test-image.png")));
final ResponseEntity<Void> updated = put("/api/v1/connectors/twitter",
multipartData, Void.class, tokenRule.validToken(), HttpStatus.NO_CONTENT,
multipartHeaders());
final ResponseEntity<Connector> updatedConnector = get("/api/v1/connectors/twitter", Connector.class);
assertThat(updatedConnector.getBody().getId()).isPresent();
assertThat(updatedConnector.getBody().getIcon()).isNotBlank().startsWith("db:");
assertThat(updatedConnector.getBody().getDescription()).isNotBlank().isEqualTo("Updated!");
final ResponseEntity<ByteArrayResource> got = get("/api/v1/connectors/twitter/icon", ByteArrayResource.class);
assertThat(got.getHeaders().getFirst("Content-Type")).isEqualTo("image/png");
try (ImageInputStream iis = ImageIO.createImageInputStream(got.getBody().getInputStream());) {
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();
}
}
}
}
示例9: mosaic
import javax.imageio.ImageReader; //导入方法依赖的package包/类
public static BufferedImage mosaic(String input, int width, int height) throws IOException {
// ��ȡͼƬ��ʽ
final String formatname = GetPostfix.fromFilepath(input);
// ͼƬ�������
final ImageReader reader = File2ImageReader.F2IR(input);
final int oriwidth = reader.getWidth(0);
final int oriheight = reader.getHeight(0);
return null;
}