一、参考项目

Poi-tl 1.9.1
Jd-gui 1.6.6
poi 4.1.2

二、代码片段与可学习的地方

1.Poi-tl: com.deepoove.poi.data.PictureType

使用到了枚举类型来管理类型

  1. /**
  2. * @author Sayi
  3. * @see org.apache.poi.xwpf.usermodel.Document
  4. */
  5. public enum PictureType {
  6. /**
  7. * Extended windows meta file
  8. */
  9. EMF(2),
  10. /**
  11. * Windows Meta File
  12. */
  13. WMF(3),
  14. /**
  15. * Mac PICT format
  16. */
  17. PICT(4),
  18. /**
  19. * JPEG format
  20. */
  21. JPEG(5),
  22. /**
  23. * PNG format
  24. */
  25. PNG(6),
  26. /**
  27. * Device independent bitmap
  28. */
  29. DIB(7),
  30. /**
  31. * GIF image format
  32. */
  33. GIF(8),
  34. /**
  35. * Tag Image File (.tiff)
  36. */
  37. TIFF(9),
  38. /**
  39. * Encapsulated Postscript (.eps)
  40. */
  41. EPS(10),
  42. /**
  43. * Windows Bitmap (.bmp)
  44. */
  45. BMP(11),
  46. /**
  47. * WordPerfect graphics (.wpg)
  48. */
  49. WPG(12);
  50. private int type;
  51. PictureType(int type) {
  52. this.type = type;
  53. }
  54. public int type() {
  55. return this.type;
  56. }
  57. public String format() {
  58. return super.name().toLowerCase();
  59. }
  60. public static PictureType suggestFileType(String imgFile) {
  61. PictureType format = null;
  62. imgFile = imgFile.toLowerCase();
  63. if (imgFile.endsWith(".emf"))
  64. format = EMF;
  65. else if (imgFile.endsWith(".wmf"))
  66. format = WMF;
  67. else if (imgFile.endsWith(".pict"))
  68. format = PICT;
  69. else if (imgFile.endsWith(".jpeg") || imgFile.endsWith(".jpg"))
  70. format = JPEG;
  71. else if (imgFile.endsWith(".png"))
  72. format = PNG;
  73. else if (imgFile.endsWith(".dib"))
  74. format = DIB;
  75. else if (imgFile.endsWith(".gif"))
  76. format = GIF;
  77. else if (imgFile.endsWith(".tiff"))
  78. format = TIFF;
  79. else if (imgFile.endsWith(".eps"))
  80. format = EPS;
  81. else if (imgFile.endsWith(".bmp"))
  82. format = BMP;
  83. else if (imgFile.endsWith(".wpg")) format = WPG;
  84. else {
  85. throw new IllegalArgumentException(
  86. "Unsupported picture: " + imgFile + ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
  87. }
  88. return format;
  89. }
  90. }