您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“java如何獲取圖片分辨率”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“java如何獲取圖片分辨率”吧!
import java.io.*; /** * 快速獲取圖片的分辨率大小 */ public class FastImageInfo { private int height; private int width; private String mimeType; public FastImageInfo(File file) throws IOException { try (InputStream is = new FileInputStream(file)) { processStream(is); } } public FastImageInfo(InputStream is) throws IOException { processStream(is); } public FastImageInfo(byte[] bytes) throws IOException { try (InputStream is = new ByteArrayInputStream(bytes)) { processStream(is); } } private void processStream(InputStream is) throws IOException { int c1 = is.read(); int c2 = is.read(); int c3 = is.read(); mimeType = null; width = height = -1; if (c1 == 'G' && c2 == 'I' && c3 == 'F') { // GIF is.skip(3); width = readInt(is, 2, false); height = readInt(is, 2, false); mimeType = "image/gif"; } else if (c1 == 0xFF && c2 == 0xD8) { // JPG while (c3 == 255) { int marker = is.read(); int len = readInt(is, 2, true); if (marker == 192 || marker == 193 || marker == 194) { is.skip(1); height = readInt(is, 2, true); width = readInt(is, 2, true); mimeType = "image/jpeg"; break; } is.skip(len - 2); c3 = is.read(); } } else if (c1 == 137 && c2 == 80 && c3 == 78) { // PNG is.skip(15); width = readInt(is, 2, true); is.skip(2); height = readInt(is, 2, true); mimeType = "image/png"; } else if (c1 == 66 && c2 == 77) { // BMP is.skip(15); width = readInt(is, 2, false); is.skip(2); height = readInt(is, 2, false); mimeType = "image/bmp"; } else if (c1 == 'R' && c2 == 'I' && c3 == 'F') { // WEBP // byte[] bytes = is.readNBytes(27); byte[] bytes = new byte[27]; is.read(bytes); width = ((int) bytes[24] & 0xff) << 8 | ((int) bytes[23] & 0xff); height = ((int) bytes[26] & 0xff) << 8 | ((int) bytes[25] & 0xff); mimeType = "image/webp"; } else { int c4 = is.read(); if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42) || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { //TIFF boolean bigEndian = c1 == 'M'; int ifd = 0; int entries; ifd = readInt(is, 4, bigEndian); is.skip(ifd - 8); entries = readInt(is, 2, bigEndian); for (int i = 1; i <= entries; i++) { int tag = readInt(is, 2, bigEndian); int fieldType = readInt(is, 2, bigEndian); int valOffset; if ((fieldType == 3 || fieldType == 8)) { valOffset = readInt(is, 2, bigEndian); is.skip(2); } else { valOffset = readInt(is, 4, bigEndian); } if (tag == 256) { width = valOffset; } else if (tag == 257) { height = valOffset; } if (width != -1 && height != -1) { mimeType = "image/tiff"; break; } } } } if (mimeType == null) { throw new IOException("Unsupported image type"); } } private int readInt(InputStream is, int noOfBytes, boolean bigEndian) throws IOException { int ret = 0; int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0; int cnt = bigEndian ? -8 : 8; for (int i = 0; i < noOfBytes; i++) { ret |= is.read() << sv; sv += cnt; } return ret; } public int getHeight() { return height; } public void setHeight(int height) { this.height = height; } public int getWidth() { return width; } public void setWidth(int width) { this.width = width; } public String getMimeType() { return mimeType; } public void setMimeType(String mimeType) { this.mimeType = mimeType; } @Override public String toString() { return "MIME Type : " + mimeType + "\t Width : " + width + "\t Height : " + height; } }
到此,相信大家對“java如何獲取圖片分辨率”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。