-
Notifications
You must be signed in to change notification settings - Fork 60
/
ImageCropViewManager.kt
108 lines (97 loc) · 3.91 KB
/
ImageCropViewManager.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.parsempo.ImageCropTools
import android.graphics.Bitmap
import android.net.Uri
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReadableArray
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.common.MapBuilder
import com.facebook.react.uimanager.SimpleViewManager
import com.facebook.react.uimanager.ThemedReactContext
import com.facebook.react.uimanager.annotations.ReactProp
import com.facebook.react.uimanager.events.RCTEventEmitter
import com.theartofdev.edmodo.cropper.CropImageView
import java.io.File
import java.util.*
class ImageCropViewManager: SimpleViewManager<CropImageView>() {
companion object {
const val REACT_CLASS = "CropView"
const val ON_IMAGE_SAVED = "onImageSaved"
const val SOURCE_URL_PROP = "sourceUrl"
const val KEEP_ASPECT_RATIO_PROP = "keepAspectRatio"
const val ASPECT_RATIO_PROP = "cropAspectRatio"
const val SAVE_IMAGE_COMMAND = 1
const val ROTATE_IMAGE_COMMAND = 2
const val SAVE_IMAGE_COMMAND_NAME = "saveImage"
const val ROTATE_IMAGE_COMMAND_NAME = "rotateImage"
}
override fun createViewInstance(reactContext: ThemedReactContext): CropImageView {
val view = CropImageView(reactContext)
view.setOnCropImageCompleteListener { _, result ->
if (result.isSuccessful) {
val map = Arguments.createMap()
map.putString("uri", result.uri.toString())
map.putInt("width", result.cropRect.width())
map.putInt("height", result.cropRect.height())
reactContext.getJSModule(RCTEventEmitter::class.java)?.receiveEvent(
view.id,
ON_IMAGE_SAVED,
map
)
}
}
return view
}
override fun getName(): String {
return REACT_CLASS
}
override fun getExportedCustomDirectEventTypeConstants(): MutableMap<String, Any> {
return MapBuilder.of(
ON_IMAGE_SAVED,
MapBuilder.of("registrationName", ON_IMAGE_SAVED)
)
}
override fun getCommandsMap(): MutableMap<String, Int> {
return MapBuilder.of(
SAVE_IMAGE_COMMAND_NAME, SAVE_IMAGE_COMMAND,
ROTATE_IMAGE_COMMAND_NAME, ROTATE_IMAGE_COMMAND
)
}
override fun receiveCommand(root: CropImageView, commandId: Int, args: ReadableArray?) {
when (commandId) {
SAVE_IMAGE_COMMAND -> {
val preserveTransparency = args?.getBoolean(0) ?: false
var extension = "jpg"
var format = Bitmap.CompressFormat.JPEG
if (preserveTransparency && root.croppedImage.hasAlpha()) {
extension = "png"
format = Bitmap.CompressFormat.PNG
}
val path = File(root.context.cacheDir, "${UUID.randomUUID()}.$extension").toURI().toString()
val quality = args?.getInt(1) ?: 100
root.saveCroppedImageAsync(Uri.parse(path), format, quality)
}
ROTATE_IMAGE_COMMAND -> {
val clockwise = args?.getBoolean(0) ?: true
root.rotateImage(if (clockwise) 90 else -90)
}
}
}
@ReactProp(name = SOURCE_URL_PROP)
fun setSourceUrl(view: CropImageView, url: String?) {
url?.let {
view.setImageUriAsync(Uri.parse(it))
}
}
@ReactProp(name = KEEP_ASPECT_RATIO_PROP)
fun setFixedAspectRatio(view: CropImageView, fixed: Boolean) {
view.setFixedAspectRatio(fixed)
}
@ReactProp(name = ASPECT_RATIO_PROP)
fun setAspectRatio(view: CropImageView, aspectRatio: ReadableMap?) {
if (aspectRatio != null) {
view.setAspectRatio(aspectRatio.getInt("width"), aspectRatio.getInt("height"))
}else {
view.clearAspectRatio()
}
}
}