ImageFormatIdentifier.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package epson.print.Util;
  2. import android.graphics.BitmapFactory;
  3. import epson.print.CommonDefine;
  4. public class ImageFormatIdentifier {
  5. public static final int FILE_FORMAT_BMP = 1;
  6. public static final int FILE_FORMAT_JPEG = 3;
  7. public static final int FILE_FORMAT_PNG = 2;
  8. public static final int FILE_FORMAT_UNKNOWN = 0;
  9. public static int identifyImageFormat(String str) {
  10. BitmapFactory.Options options = new BitmapFactory.Options();
  11. options.inJustDecodeBounds = true;
  12. BitmapFactory.decodeFile(str, options);
  13. int mimeTypeToInt = mimeTypeToInt(options);
  14. if (mimeTypeToInt != 0) {
  15. return mimeTypeToInt;
  16. }
  17. return localIdentifyImageFormat(str);
  18. }
  19. public static int mimeTypeToInt(BitmapFactory.Options options) {
  20. if (options == null || options.outMimeType == null) {
  21. return 0;
  22. }
  23. String str = options.outMimeType;
  24. if (CommonDefine.IMAGE_TYPE_BMP.equalsIgnoreCase(str)) {
  25. return 1;
  26. }
  27. if ("image/png".equalsIgnoreCase(str)) {
  28. return 2;
  29. }
  30. if ("image/jpeg".equalsIgnoreCase(str)) {
  31. return 3;
  32. }
  33. return 0;
  34. }
  35. public static int localIdentifyImageFormat(String str) {
  36. return new BmpFileInfo().readParams(str) ? 1 : 0;
  37. }
  38. }