ImageInfo.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // package XXX
  2. import java.io.ByteArrayInputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. @SuppressWarnings("all")
  8. public class ImageInfo {
  9. int height; // private
  10. int width; // private
  11. String mimeType;// private
  12. private ImageInfo() {}
  13. public ImageInfo(File file) throws IOException {
  14. FileInputStream fileInputStream = null;
  15. try {
  16. fileInputStream = new FileInputStream(file);
  17. processStream(fileInputStream);
  18. } catch(IOException e){
  19. System.out.println("Error: " + e + "\n" + "Program ist sofort beendet !");
  20. System.exit(0);
  21. } finally {
  22. try {
  23. if (fileInputStream != null)
  24. fileInputStream.close();
  25. } catch (IOException e) {
  26. System.out.println("Error: " + e + "\n" + "Program ist sofort beendet !");
  27. System.exit(0);
  28. }
  29. }
  30. }
  31. public ImageInfo(InputStream inputStream) throws IOException {
  32. processStream(inputStream);
  33. }
  34. public ImageInfo(byte[] bytes) throws IOException {
  35. InputStream inputStream = new ByteArrayInputStream(bytes);
  36. try {
  37. processStream(inputStream);
  38. } finally {
  39. inputStream.close();
  40. }
  41. }
  42. private void processStream(InputStream inputStream) throws IOException {
  43. int c1 = inputStream.read();
  44. int c2 = inputStream.read();
  45. int c3 = inputStream.read();
  46. mimeType = null;
  47. width = height = -1;
  48. if (c1 == 'G' && c2 == 'I' && c3 == 'F') { // GIF
  49. inputStream.skip(3);
  50. width = readInt(inputStream,2,false);
  51. height = readInt(inputStream,2,false);
  52. mimeType = "image/gif";
  53. } else if (c1 == 0xFF && c2 == 0xD8) { // JPG
  54. while (c3 == 255) {
  55. int marker = inputStream.read();
  56. int len = readInt(inputStream,2,true);
  57. if (marker == 192 || marker == 193 || marker == 194) {
  58. inputStream.skip(1);
  59. height = readInt(inputStream,2,true);
  60. width = readInt(inputStream,2,true);
  61. mimeType = "image/jpeg";
  62. break;
  63. }
  64. inputStream.skip(len - 2);
  65. c3 = inputStream.read();
  66. }
  67. } else if (c1 == 137 && c2 == 80 && c3 == 78) { // PNG
  68. inputStream.skip(15);
  69. width = readInt(inputStream,2,true);
  70. inputStream.skip(2);
  71. height = readInt(inputStream,2,true);
  72. mimeType = "image/png";
  73. } else if (c1 == 66 && c2 == 77) { // BMP
  74. inputStream.skip(15);
  75. width = readInt(inputStream,2,false);
  76. inputStream.skip(2);
  77. height = readInt(inputStream,2,false);
  78. mimeType = "image/bmp";
  79. } else {
  80. int c4 = inputStream.read();
  81. if ((c1 == 'M' && c2 == 'M' && c3 == 0 && c4 == 42)
  82. || (c1 == 'I' && c2 == 'I' && c3 == 42 && c4 == 0)) { //TIFF
  83. boolean bigEndian = c1 == 'M';
  84. int ifd = 0;
  85. int entries;
  86. ifd = readInt(inputStream,4,bigEndian);
  87. inputStream.skip(ifd - 8);
  88. entries = readInt(inputStream,2,bigEndian);
  89. for (int i = 1; i <= entries; i++) {
  90. int tag = readInt(inputStream,2,bigEndian);
  91. int fieldType = readInt(inputStream,2,bigEndian);
  92. long count = readInt(inputStream,4,bigEndian);
  93. int valOffset;
  94. if ((fieldType == 3 || fieldType == 8)) {
  95. valOffset = readInt(inputStream,2,bigEndian);
  96. inputStream.skip(2);
  97. } else {
  98. valOffset = readInt(inputStream,4,bigEndian);
  99. }
  100. if (tag == 256) {
  101. width = valOffset;
  102. } else if (tag == 257) {
  103. height = valOffset;
  104. }
  105. if (width != -1 && height != -1) {
  106. mimeType = "image/tiff";
  107. break;
  108. }
  109. }
  110. }
  111. }
  112. if (mimeType == null) {
  113. throw new IOException("Unsupported image type");
  114. }
  115. }
  116. private int readInt(InputStream is, int noOfBytes, boolean bigEndian) throws IOException {
  117. int ret = 0;
  118. int sv = bigEndian ? ((noOfBytes - 1) * 8) : 0;
  119. int cnt = bigEndian ? -8 : 8;
  120. for(int i=0;i<noOfBytes;i++) {
  121. ret |= is.read() << sv;
  122. sv += cnt;
  123. }
  124. return ret;
  125. }
  126. public int getHeight() {
  127. return height;
  128. }
  129. public void setHeight(int height) {
  130. this.height = height;
  131. }
  132. public int getWidth() {
  133. return width;
  134. }
  135. public void setWidth(int width) {
  136. this.width = width;
  137. }
  138. public String getMimeType() {
  139. return mimeType;
  140. }
  141. public void setMimeType(String mimeType) {
  142. this.mimeType = mimeType;
  143. }
  144. @Override
  145. public String toString() {
  146. return "MIME Type : " + mimeType + "\t Width : " + width + "\t Height : " + height;
  147. }
  148. }