imageTools.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. function getLocalFilePath(path) {
  2. if (path.indexOf('_www') === 0 || path.indexOf('_doc') === 0 || path.indexOf('_documents') === 0 || path.indexOf('_downloads') === 0) {
  3. return path
  4. }
  5. if (path.indexOf('file://') === 0) {
  6. return path
  7. }
  8. if (path.indexOf('/storage/emulated/0/') === 0) {
  9. return path
  10. }
  11. if (path.indexOf('/') === 0) {
  12. var localFilePath = plus.io.convertAbsoluteFileSystem(path)
  13. if (localFilePath !== path) {
  14. return localFilePath
  15. } else {
  16. path = path.substr(1)
  17. }
  18. }
  19. return '_www/' + path
  20. }
  21. export function pathToBase64(path) {
  22. return new Promise(function(resolve, reject) {
  23. if (typeof window === 'object' && 'document' in window) {
  24. var canvas = document.createElement('canvas')
  25. var c2x = canvas.getContext('2d')
  26. var img = new Image
  27. img.onload = function() {
  28. canvas.width = img.width
  29. canvas.height = img.height
  30. c2x.drawImage(img, 0, 0)
  31. resolve(canvas.toDataURL())
  32. }
  33. img.onerror = reject
  34. img.src = path
  35. return
  36. }
  37. if (typeof plus === 'object') {
  38. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  39. entry.file(function(file) {
  40. var fileReader = new plus.io.FileReader()
  41. fileReader.onload = function(data) {
  42. resolve(data.target.result)
  43. }
  44. fileReader.onerror = function(error) {
  45. reject(error)
  46. }
  47. fileReader.readAsDataURL(file)
  48. }, function(error) {
  49. reject(error)
  50. })
  51. }, function(error) {
  52. reject(error)
  53. })
  54. return
  55. }
  56. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  57. wx.getFileSystemManager().readFile({
  58. filePath: path,
  59. encoding: 'base64',
  60. success: function(res) {
  61. resolve('data:image/png;base64,' + res.data)
  62. },
  63. fail: function(error) {
  64. reject(error)
  65. }
  66. })
  67. return
  68. }
  69. reject(new Error('not support'))
  70. })
  71. }
  72. export function base64ToPath(base64) {
  73. return new Promise(function(resolve, reject) {
  74. if (typeof window === 'object' && 'document' in window) {
  75. base64 = base64.split(',')
  76. var type = base64[0].match(/:(.*?);/)[1]
  77. var str = atob(base64[1])
  78. var n = str.length
  79. var array = new Uint8Array(n)
  80. while (n--) {
  81. array[n] = str.charCodeAt(n)
  82. }
  83. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
  84. }
  85. var extName = base64.match(/data\:\S+\/(\S+);/)
  86. if (extName) {
  87. extName = extName[1]
  88. } else {
  89. reject(new Error('base64 error'))
  90. }
  91. var fileName = Date.now() + '.' + extName
  92. if (typeof plus === 'object') {
  93. var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  94. bitmap.loadBase64Data(base64, function() {
  95. var filePath = '_doc/uniapp_temp/' + fileName
  96. bitmap.save(filePath, {}, function() {
  97. bitmap.clear()
  98. resolve(filePath)
  99. }, function(error) {
  100. bitmap.clear()
  101. reject(error)
  102. })
  103. }, function(error) {
  104. bitmap.clear()
  105. reject(error)
  106. })
  107. return
  108. }
  109. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  110. var filePath = wx.env.USER_DATA_PATH + '/' + fileName
  111. wx.getFileSystemManager().writeFile({
  112. filePath: filePath,
  113. data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
  114. encoding: 'base64',
  115. success: function() {
  116. resolve(filePath)
  117. },
  118. fail: function(error) {
  119. reject(error)
  120. }
  121. })
  122. return
  123. }
  124. reject(new Error('not support'))
  125. })
  126. }