Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- public class SimpleImageInfo {
- private final int _width;
- private final int _height;
- private final String type;
- private SimpleImageInfo(final int width, final int height, final String type) {
- _width = width;
- _height = height;
- this.type = type;
- }
- @Override
- public String toString() {
- return "SimpleImageInfo{" + "_width=" + _width + ", _height=" + _height + ", type='" + type + '\'' + '}';
- }
- @Override
- public boolean equals(final Object o) {
- if (this == o) return true;
- if (!(o instanceof SimpleImageInfo)) return false;
- final SimpleImageInfo that = (SimpleImageInfo) o;
- if (_width != that._width) return false;
- if (_height != that._height) return false;
- return type != null ? type.equals(that.type) : that.type == null;
- }
- @Override
- public int hashCode() {
- int result = _width;
- result = 31 * result + _height;
- result = 31 * result + (type != null ? type.hashCode() : 0);
- return result;
- }
- public int getWidth() {
- return _width;
- }
- public int getHeight() {
- return _height;
- }
- public String getType() {
- return type;
- }
- private interface StreamSupplier {
- int read() throws IOException;
- long skip(long skip) throws IOException;
- }
- public static SimpleImageInfo getInfo(File file, File target) throws IOException {
- final InputStream is = new FileInputStream(file);
- try {
- return getInfo(is, target);
- } finally {
- is.close();
- }
- }
- public static SimpleImageInfo getInfo(InputStream inputStream, File target) throws IOException {
- final OutputStream outputStream = new FileOutputStream(target);
- try {
- return getInfo(inputStream, outputStream);
- } finally {
- outputStream.close();
- }
- }
- public static SimpleImageInfo getInfo(byte[] bytes, OutputStream outputStream) throws IOException {
- final InputStream is = new ByteArrayInputStream(bytes);
- try {
- return getInfo(is, outputStream);
- } finally {
- is.close();
- }
- }
- public static SimpleImageInfo getInfo(final InputStream is, final OutputStream outputStream) throws IOException {
- final SimpleImageInfo info = getInfo(new StreamSupplier() {
- @Override
- public int read() throws IOException {
- final int read = is.read();
- outputStream.write(read);
- return read;
- }
- @Override
- public long skip(final long n) throws IOException {
- if (n <= 0) {
- return 0;
- }
- long remaining = n;
- final int size = (int) Math.min(2048, remaining);
- final byte[] skipBuffer = new byte[size];
- while (remaining > 0) {
- final int length = (int) Math.min(size, remaining);
- final int nr = is.read(skipBuffer, 0, length);
- if (nr < 0) {
- break;
- }
- outputStream.write(skipBuffer, 0, nr);
- remaining -= nr;
- }
- return n - remaining;
- }
- });
- final byte[] buffer = new byte[2048];
- int n;
- while (-1 != (n = is.read(buffer))) {
- outputStream.write(buffer, 0, n);
- }
- return info;
- }
- public static SimpleImageInfo getInfo(File file) throws IOException {
- final InputStream is = new FileInputStream(file);
- try {
- return getInfo(is);
- } finally {
- is.close();
- }
- }
- public static SimpleImageInfo getInfo(byte[] bytes) throws IOException {
- final InputStream is = new ByteArrayInputStream(bytes);
- try {
- return getInfo(is);
- } finally {
- is.close();
- }
- }
- /**
- * Based on SimplaImageInfo class from Jaimon Mathew <http://www.jaimon.co.uk>
- * http://blog.jaimon.co.uk/simpleimageinfo/SimpleImageInfo.java.html
- */
- public static SimpleImageInfo getInfo(final InputStream is) throws IOException {
- return getInfo(new StreamSupplier() {
- @Override
- public int read() throws IOException {
- return is.read();
- }
- @Override
- public long skip(final long skip) throws IOException {
- return is.skip(skip);
- }
- });
- }
- private static SimpleImageInfo getInfo(StreamSupplier is) throws IOException {
- final int h1 = is.read();
- final int h2 = is.read();
- if (h1 == 0xFF && h2 == 0xD8) { //JPG
- return getJPG(is);
- } else if (h1 == 66 && h2 == 77) { // BMP
- return getBMP(is);
- } else {
- final int h3 = is.read();
- if (h1 == 137 && h2 == 80 && h3 == 78) { // PNG
- return getPNG(is);
- } else if (h1 == 'G' && h2 == 'I' && h3 == 'F') { // GIF
- return getGIF(is);
- } else {
- final int c4 = is.read();
- if ((h1 == 'M' && h2 == 'M' && h3 == 0 && c4 == 42) || (h1 == 'I' && h2 == 'I' && h3 == 42 && c4 == 0)) { //TIFF
- final boolean bigEndian = h1 == 'M';
- return getTIFF(is, bigEndian);
- } else {
- throw new IOException("Unsupported Image Type");
- }
- }
- }
- }
- private static SimpleImageInfo getJPG(final StreamSupplier is) throws IOException {
- int currentRead = is.read();
- while (currentRead == 255) {
- final int marker = is.read();
- final int len = readInt(is, 2, true);
- if (marker == 192 || marker == 193 || marker == 194) {
- is.skip(1);
- final int height = readInt(is, 2, true);
- final int width = readInt(is, 2, true);
- return new SimpleImageInfo(width, height, "JPG");
- } else {
- is.skip(len - 2);
- currentRead = is.read();
- }
- }
- throw new IOException("Unsupported Jpg format");
- }
- private static SimpleImageInfo getTIFF(final StreamSupplier is, final boolean bigEndian) throws IOException {
- int height = -1;
- int width = -1;
- final int ifd = readInt(is, 4, bigEndian);
- is.skip(ifd - 8);
- final int entries = readInt(is, 2, bigEndian);
- for (int i = 1; i <= entries; i++) {
- final int tag = readInt(is, 2, bigEndian);
- final int fieldType = readInt(is, 2, bigEndian);
- final long count = readInt(is, 4, bigEndian);
- final 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) {
- return new SimpleImageInfo(width, height, "TIF");
- }
- }
- throw new IOException("Unsupported Tiff Format");
- }
- private static SimpleImageInfo getBMP(final StreamSupplier is) throws IOException {
- is.skip(16);
- final int width = readInt(is, 2, false);
- is.skip(2);
- final int height = readInt(is, 2, false);
- return new SimpleImageInfo(width, height, "BMP");
- }
- private static SimpleImageInfo getPNG(final StreamSupplier is) throws IOException {
- is.skip(15);
- final int width = readInt(is, 2, true);
- is.skip(2);
- final int height = readInt(is, 2, true);
- return new SimpleImageInfo(width, height, "PNG");
- }
- private static SimpleImageInfo getGIF(final StreamSupplier is) throws IOException {
- is.skip(3);
- final int width = readInt(is, 2, false);
- final int height = readInt(is, 2, false);
- return new SimpleImageInfo(width, height, "GIF");
- }
- private static int readInt(StreamSupplier is, int noOfBytes, boolean bigEndian) throws IOException {
- int result = 0;
- int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
- final int cnt = bigEndian ? -8 : 8;
- for (int i = 0; i < noOfBytes; i++) {
- result |= is.read() << sv;
- sv += cnt;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement