Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

image-tools.js 5.2 KiB

2 anni fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. if (typeof FileReader === 'function') {
  25. var xhr = new XMLHttpRequest()
  26. xhr.open('GET', path, true)
  27. xhr.responseType = 'blob'
  28. xhr.onload = function() {
  29. if (this.status === 200) {
  30. let fileReader = new FileReader()
  31. fileReader.onload = function(e) {
  32. resolve(e.target.result)
  33. }
  34. fileReader.onerror = reject
  35. fileReader.readAsDataURL(this.response)
  36. }
  37. }
  38. xhr.onerror = reject
  39. xhr.send()
  40. return
  41. }
  42. var canvas = document.createElement('canvas')
  43. var c2x = canvas.getContext('2d')
  44. var img = new Image
  45. img.onload = function() {
  46. canvas.width = img.width
  47. canvas.height = img.height
  48. c2x.drawImage(img, 0, 0)
  49. resolve(canvas.toDataURL())
  50. canvas.height = canvas.width = 0
  51. }
  52. img.onerror = reject
  53. img.src = path
  54. return
  55. }
  56. if (typeof plus === 'object') {
  57. plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), function(entry) {
  58. entry.file(function(file) {
  59. var fileReader = new plus.io.FileReader()
  60. fileReader.onload = function(data) {
  61. resolve(data.target.result)
  62. }
  63. fileReader.onerror = function(error) {
  64. reject(error)
  65. }
  66. fileReader.readAsDataURL(file)
  67. }, function(error) {
  68. reject(error)
  69. })
  70. }, function(error) {
  71. reject(error)
  72. })
  73. return
  74. }
  75. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  76. wx.getFileSystemManager().readFile({
  77. filePath: path,
  78. encoding: 'base64',
  79. success: function(res) {
  80. resolve('data:image/png;base64,' + res.data)
  81. },
  82. fail: function(error) {
  83. reject(error)
  84. }
  85. })
  86. return
  87. }
  88. reject(new Error('not support'))
  89. })
  90. }
  91. export function base64ToPath(base64, extName) {
  92. return new Promise(function(resolve, reject) {
  93. if (typeof window === 'object' && 'document' in window) {
  94. base64 = base64.split(',')
  95. var type = base64[0].match(/:(.*?);/)[1]
  96. var str = atob(base64[1])
  97. var n = str.length
  98. var array = new Uint8Array(n)
  99. while (n--) {
  100. array[n] = str.charCodeAt(n)
  101. }
  102. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([array], { type: type })))
  103. }
  104. var fileName;
  105. if (!extName) {
  106. extName = base64.match(/data\:\S+\/(\S+);/)
  107. if (extName) {
  108. extName = extName[1]
  109. } else {
  110. reject(new Error('base64 error'))
  111. }
  112. fileName = Date.now() + '.' + extName;
  113. } else {
  114. fileName = Date.now() + extName;
  115. }
  116. if (typeof plus === 'object') {
  117. var bitmap = new plus.nativeObj.Bitmap('bitmap' + Date.now())
  118. bitmap.loadBase64Data(base64, function() {
  119. var filePath = '_doc/uniapp_temp/' + fileName
  120. bitmap.save(filePath, {}, function() {
  121. bitmap.clear()
  122. resolve(filePath)
  123. }, function(error) {
  124. bitmap.clear()
  125. reject(error)
  126. })
  127. }, function(error) {
  128. bitmap.clear()
  129. reject(error)
  130. })
  131. return
  132. }
  133. if (typeof wx === 'object' && wx.canIUse('getFileSystemManager')) {
  134. var filePath = wx.env.USER_DATA_PATH + '/' + fileName
  135. wx.getFileSystemManager().writeFile({
  136. filePath: filePath,
  137. data: base64.replace(/^data:\S+\/\S+;base64,/, ''),
  138. encoding: 'base64',
  139. success: function() {
  140. resolve(filePath)
  141. },
  142. fail: function(error) {
  143. reject(error)
  144. }
  145. })
  146. return
  147. }
  148. reject(new Error('not support'))
  149. })
  150. }