Skip to content

Commit 6f98c4c

Browse files
author
houningjuanNew
committed
change version
1 parent d2378fd commit 6f98c4c

File tree

6 files changed

+7873
-7804
lines changed

6 files changed

+7873
-7804
lines changed

harmony/image_editor.har

2.79 KB
Binary file not shown.

harmony/image_editor/README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@
2323
#### **npm**
2424

2525
```bash
26-
npm install @react-native-oh-tpl/image-editor
26+
npm install @react-native-oh-tpl/image-editor@file:#
2727
```
2828

2929
#### **yarn**
3030

3131
```bash
32-
yarn add @react-native-oh-tpl/image-editor
32+
yarn add @react-native-oh-tpl/image-editor@file:#
3333
```
3434

3535

@@ -68,7 +68,7 @@ ImageEditor.cropImage(uri, cropData).then((result) => {
6868
```json
6969
"dependencies": {
7070
"rnoh": "file:../rnoh",
71-
"rnoh-xxx": "file:../../node_modules/@react-native-oh-tpl/image-editor/harmony/image_editor.har"
71+
"rnoh-image-editor": "file:../../node_modules/@react-native-oh-tpl/image-editor/harmony/image_editor.har"
7272
}
7373
```
7474

@@ -90,7 +90,7 @@ ohpm install
9090
```json
9191
"dependencies": {
9292
"rnoh": "file:../rnoh",
93-
"rnoh-xxx": "file:../../node_modules/@react-native-oh-tpl/image-editor/harmony/image_editor"
93+
"rnoh-image-editor": "file:../../node_modules/@react-native-oh-tpl/image-editor/harmony/image_editor"
9494
}
9595
```
9696

@@ -101,7 +101,7 @@ cd entry
101101
ohpm install --no-link
102102
```
103103

104-
### 配置 CMakeLists 和引入 xxxPackge
104+
### 配置 CMakeLists 和引入ImageEditorPackage
105105

106106
打开 `entry/src/main/cpp/CMakeLists.txt`,添加:
107107

harmony/image_editor/oh-package.json5

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rnoh-image-editor",
3-
"version": "1.0.0",
3+
"version": "3.2.0-0.0.1",
44
"description": "Please describe the basic information.",
55
"main": "index.ets",
66
"author": "",

harmony/image_editor/src/main/ets/ImageEditorModule.ts

Lines changed: 57 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -62,72 +62,58 @@ interface EditorResult {
6262
base64?: string
6363
}
6464

65-
const context = getContext(this) as common.ApplicationContext
66-
67-
let newOptions: ImageCropData = {
68-
offset: {x: 0, y: 0},
69-
size: {width: 0, height: 0},
70-
resizeMode: 'cover',
71-
includeBase64: false,
72-
}
73-
74-
let resizeScaleSize: size = {
75-
width: 0,
76-
height: 0
77-
}
78-
7965
async function loadBase(uri: string) {
8066
return new Promise((resolve)=>{
8167
let buf = buffer.alloc(uri.length, uri)
8268
resolve(buf.buffer)
8369
})
8470
}
71+
8572
function loadHttp(uri: string) {
8673
return new Promise((resolve, reject)=>{
8774
http.createHttp().request(uri,{
8875
header: {
8976
'Content-Type': 'application/octet-stream'
9077
}
9178
},
92-
async (error: BusinessError, data: http.HttpResponse) => {
93-
let code: http.ResponseCode | number = data.responseCode
94-
if (ResponseCode.ResponseCode.OK === code) {
95-
const imageData = data.result as ArrayBuffer
96-
Logger.info("http.createHttp success")
97-
resolve(imageData)
98-
} else {
99-
Logger.error("http.createHttp error is " + error)
100-
}
101-
})
79+
async (error: BusinessError, data: http.HttpResponse) => {
80+
let code: http.ResponseCode | number = data.responseCode
81+
if (ResponseCode.ResponseCode.OK === code) {
82+
const imageData = data.result as ArrayBuffer
83+
Logger.info("http.createHttp success")
84+
resolve(imageData)
85+
} else {
86+
Logger.error("http.createHttp error is " + error)
87+
}
88+
})
10289
})
10390
}
10491

105-
async function getUriBuffer(uri: string) {
106-
let imageBuffer = null
92+
async function imageEditor(newOptions:ImageCropData, uri: string): Promise<EditorResult> {
93+
let imageData = null
94+
let openFile = null
10795
if(uri.startsWith("data:")){
108-
imageBuffer = await loadBase(uri)
96+
imageData = await loadBase(uri)
10997
} else if(uri.startsWith('http')) {
110-
imageBuffer = await loadHttp(uri)
98+
imageData = await loadHttp(uri)
11199
} else {
112-
imageBuffer = uri
100+
openFile = fs.openSync(uri, fs.OpenMode.READ_ONLY)
101+
imageData = openFile.fd
113102
}
114-
return imageBuffer
115-
}
116103

117-
async function imageEditor(imageData): Promise<EditorResult> {
118104
let imageSourceApi: image.ImageSource = image.createImageSource(imageData)
119105
let getImageInfo: image.ImageInfo
120106
let options: Record<string, number | boolean> = {
121107
'editable': true,
122108
}
123-
let editorPM = await imageSourceApi.createPixelMap(options)
109+
let editorPM = await imageSourceApi.createPixelMap(options)
124110

125111
editorPM.getImageInfo().then((imageInfo: image.ImageInfo) => {
126-
if(newOptions.offset.x+newOptions.size.width > imageInfo.size.width || newOptions.offset.y+newOptions.size.height > imageInfo.size.height){
112+
if(newOptions.offset.x+newOptions.size.width > imageInfo.size.width || newOptions.offset.y+newOptions.size.height > imageInfo.size.height){
127113
Logger.error('[RNOH]:The cropped size exceeds the original size')
128114
return
129115
}
130-
})
116+
})
131117

132118
const x = newOptions.offset.x
133119
const y = newOptions.offset.y
@@ -136,24 +122,24 @@ async function imageEditor(imageData): Promise<EditorResult> {
136122
let region: image.Region = { x, y, size: {height, width}}
137123

138124
await editorPM.crop(region).then(() => {
139-
Logger.info('imageEditor.Succeeded in crop.');
140-
}).catch((error: BusinessError) => {
141-
Logger.error('imageEditor.Failed to crop.');
142-
})
125+
Logger.info('imageEditor.Succeeded in crop.')
126+
}).catch((error: BusinessError) => {
127+
Logger.error('imageEditor.Failed to crop.')
128+
})
143129

144130
if(newOptions.displaySize && newOptions.displaySize.width && newOptions.displaySize.height){
145-
146131
const cropSize = newOptions.size
147132
const displaySize = JSON.parse(JSON.stringify(newOptions.displaySize))
148133
const aspect = cropSize.width / cropSize.height
149134
const targetAspect = displaySize.width / displaySize.height
150135
if(aspect === targetAspect) newOptions.resizeMode = 'stretch'
151-
136+
152137
const xRatio = displaySize.width / cropSize.width
153138
const yRatio = displaySize.height / cropSize.height
154139
if(newOptions.resizeMode === 'stretch'){
155140
await editorPM.scale(xRatio, yRatio)
156141
} else if(newOptions.resizeMode === 'cover'){
142+
let resizeScaleSize: size = displaySize
157143
if(displaySize.width !== cropSize.width || displaySize.height !== cropSize.height){
158144
const ratio = Math.max(xRatio, yRatio)
159145
await editorPM.scale(ratio, ratio)
@@ -163,40 +149,42 @@ async function imageEditor(imageData): Promise<EditorResult> {
163149
})
164150
}
165151

166-
const targetRegion = await TargetRect()
152+
const targetRegion = await TargetRect(newOptions, resizeScaleSize)
167153
await editorPM.crop(targetRegion).then(() => {
168-
Logger.info('imageEditor.Succeeded in crop.');
154+
Logger.info('imageEditor.Succeeded in crop.')
169155
}).catch((error: BusinessError) => {
170-
Logger.error('imageEditor.Failed to crop.');
156+
Logger.error('imageEditor.Failed to crop.')
171157
})
172-
173158
} else {
174-
const { size } = await TargetRect()
159+
const { size } = await TargetRect(newOptions)
175160
const xRatio = size.width / cropSize.width
176161
const yRatio = size.height / cropSize.height
177162
await editorPM.scale(xRatio, yRatio)
178163
}
179-
180164
}
181165
await editorPM.getImageInfo().then((imageInfo: image.ImageInfo) => {
182-
getImageInfo = imageInfo
183-
})
184-
166+
getImageInfo = imageInfo
167+
})
168+
let context = getContext(this) as common.ApplicationContext
185169
let suffix = newOptions.format
186170
suffix = suffix === 'jpeg' || suffix === 'jpg' ? 'jpeg' : suffix
187171
const fileName = `ReactNative_cropped_image_${new Date().getTime()}.${suffix==='jpeg'?'jpg':suffix}`
188172
const path: string = `${context.cacheDir}/${fileName}`
189173
let packOpts: image.PackingOption = { format: `image/${suffix}`, quality: newOptions.quality || 90 }
190-
let file = await fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
191174
let size = 0
192175
let base64Data = ''
193176

194177
const imagePackerApi:image.ImagePacker = image.createImagePacker()
195178
await imagePackerApi.packing(editorPM, packOpts)
196179
.then(async (data: ArrayBuffer) => {
197-
let writeLen = await fs.writeSync(file.fd, data)
198-
size = writeLen
199-
fs.closeSync(file)
180+
let file = fs.openSync(path, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE)
181+
try {
182+
size = fs.writeSync(file.fd, data)
183+
} catch(err) {
184+
Logger.error('file write failed')
185+
} finally {
186+
fs.closeSync(file)
187+
}
200188

201189
if(newOptions.includeBase64){
202190
let unit8Array: Uint8Array = new Uint8Array(data)
@@ -205,14 +193,15 @@ async function imageEditor(imageData): Promise<EditorResult> {
205193
}
206194

207195
}).catch((error: BusinessError) => {
208-
Logger.error('packing failed.');
196+
Logger.error('packing failed.')
197+
}).finally(() => {
198+
openFile && fs.closeSync(openFile)
209199
})
210200

211201
editorPM.release()
212202
imageSourceApi.release()
213203
imagePackerApi.release()
214-
215-
size = await fs.statSync(path).size;
204+
context = null
216205

217206
const result:EditorResult = {
218207
uri: `file://${path}`,
@@ -226,10 +215,10 @@ async function imageEditor(imageData): Promise<EditorResult> {
226215
if(newOptions.includeBase64){
227216
result.base64 = base64Data
228217
}
229-
return result;
218+
return result
230219
}
231220

232-
async function TargetRect() {
221+
async function TargetRect(newOptions: ImageCropData, resizeScaleSize?: size) {
233222
const resizeMode = newOptions.resizeMode
234223
const cropSize = newOptions.size
235224
const displaySize = JSON.parse(JSON.stringify(newOptions.displaySize))
@@ -255,17 +244,18 @@ async function TargetRect() {
255244
targetSize.height = displaySize.height
256245
targetSize.width = Math.ceil(targetSize.height * aspect)
257246
}
247+
targetRegion.size = targetSize
258248
} else if(resizeMode === 'center') {
259249
if(cropSize.height > displaySize.height) {
260250
targetSize.width = displaySize.width
261251
targetSize.height = Math.ceil(targetSize.width / aspect)
262-
}
252+
}
263253
if(cropSize.width > displaySize.width) {
264254
targetSize.height = displaySize.height
265255
targetSize.width = Math.ceil(targetSize.height * aspect)
266256
}
257+
targetRegion.size = targetSize
267258
}
268-
targetRegion.size = targetSize
269259
return targetRegion
270260
}
271261

@@ -278,18 +268,18 @@ export class ImageEditorModule extends TurboModule {
278268
quality = Math.floor(options.quality * 100)
279269
}
280270
if(!uri){
281-
Logger.warn('[RNOH]:Please specify a URI');
271+
Logger.warn('[RNOH]:Please specify a URI')
282272
return
283273
}
284274
if(!offset || !size || !('x' in offset) || !('y' in offset) || !size.width || !size.height){
285-
Logger.warn('[RNOH]:Please specify offset and size');
275+
Logger.warn('[RNOH]:Please specify offset and size')
286276
return
287277
}
288278
if(quality > 100 || quality < 0){
289-
Logger.warn('[RNOH]:quality must be a number between 0 and 1');
279+
Logger.warn('[RNOH]:quality must be a number between 0 and 1')
290280
return
291281
}
292-
282+
let newOptions: ImageCropData = options
293283
newOptions.size = size
294284
newOptions.offset = offset
295285
newOptions.quality = quality
@@ -303,10 +293,7 @@ export class ImageEditorModule extends TurboModule {
303293
}
304294
if(options.includeBase64) newOptions.includeBase64 = options.includeBase64
305295

306-
const buffer = await getUriBuffer(uri)
307-
const fileUri: EditorResult = await imageEditor(buffer)
308-
296+
const fileUri: EditorResult = await imageEditor(newOptions, uri)
309297
return fileUri.uri
310-
311298
}
312-
}
299+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-native-oh-tpl/image-editor",
3-
"version": "3.2.0",
3+
"version": "3.2.0-0.0.1",
44
"description": "React Native Image Editing native modules for iOS & Android & Harmony",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)