|
1 | | -//package com.reactnativemultipleimagepicker |
2 | | -// |
3 | | -//import android.content.Context |
4 | | -//import android.graphics.Bitmap |
5 | | -//import android.net.Uri |
6 | | -//import android.widget.ImageView |
7 | | -//import androidx.core.content.ContextCompat |
8 | | -//import androidx.fragment.app.Fragment |
9 | | -//import com.bumptech.glide.Glide |
10 | | -//import com.luck.picture.lib.engine.CropFileEngine |
11 | | -//import com.luck.picture.lib.interfaces.OnCallbackListener |
12 | | -//import com.luck.picture.lib.style.TitleBarStyle |
13 | | -//import com.luck.picture.lib.utils.StyleUtils |
14 | | -//import com.yalantis.ucrop.UCrop |
15 | | -//import com.yalantis.ucrop.UCropImageEngine |
16 | | -//import java.security.AccessController.getContext |
17 | | -// |
18 | | -//class ImageFileCropEngine : CropFileEngine { |
19 | | -// fun onStartCrop( |
20 | | -// fragment: Fragment, |
21 | | -// srcUri: Uri?, |
22 | | -// destinationUri: Uri?, |
23 | | -// dataSource: ArrayList<String?>?, |
24 | | -// requestCode: Int |
25 | | -// ) { |
26 | | -// val options: UCrop.Options = buildOptions() |
27 | | -// val uCrop: UCrop = UCrop.of(srcUri, destinationUri, dataSource) |
28 | | -// uCrop.withOptions(options) |
29 | | -// uCrop.setImageEngine(object : UCropImageEngine() { |
30 | | -// fun loadImage(context: Context?, url: String?, imageView: ImageView?) { |
31 | | -// if (!ImageLoaderUtils.assertValidRequest(context)) { |
32 | | -// return |
33 | | -// } |
34 | | -// GlideEngine.with(context).load(url).override(180, 180).into(imageView) |
35 | | -// } |
36 | | -// |
37 | | -// fun loadImage( |
38 | | -// context: Context?, |
39 | | -// url: Uri?, |
40 | | -// maxWidth: Int, |
41 | | -// maxHeight: Int, |
42 | | -// call: OnCallbackListener<Bitmap?>? |
43 | | -// ) { |
44 | | -// Glide.with(context).asBitmap().load(url).override(maxWidth, maxHeight) |
45 | | -// .into(object : CustomTarget<Bitmap?>() { |
46 | | -// fun onResourceReady( |
47 | | -// @NonNull resource: Bitmap?, |
48 | | -// @Nullable transition: Transition<in Bitmap?>? |
49 | | -// ) { |
50 | | -// if (call != null) { |
51 | | -// call.onCall(resource) |
52 | | -// } |
53 | | -// } |
54 | | -// |
55 | | -// fun onLoadCleared(@Nullable placeholder: Drawable?) { |
56 | | -// if (call != null) { |
57 | | -// call.onCall(null) |
58 | | -// } |
59 | | -// } |
60 | | -// }) |
61 | | -// } |
62 | | -// }) |
63 | | -// uCrop.start(fragment.requireActivity(), fragment, requestCode) |
64 | | -// } |
65 | | -//} |
66 | | -// |
67 | | -//private fun buildOptions(): UCrop.Options { |
68 | | -// val options = UCrop.Options() |
69 | | -//// options.setHideBottomControls(!cb_hide.isChecked()) |
70 | | -//// options.setFreeStyleCropEnabled(cb_styleCrop.isChecked()) |
71 | | -//// options.setShowCropFrame(cb_showCropFrame.isChecked()) |
72 | | -//// options.setShowCropGrid(cb_showCropGrid.isChecked()) |
73 | | -//// options.setCircleDimmedLayer(cb_crop_circular.isChecked()) |
74 | | -//// options.withAspectRatio(aspect_ratio_x, aspect_ratio_y) |
75 | | -//// options.setCropOutputPathDir(getSandboxPath()) |
76 | | -//// options.isCropDragSmoothToCenter(false) |
77 | | -//// options.setSkipCropMimeType(getNotSupportCrop()) |
78 | | -//// options.isForbidCropGifWebp(cb_not_gif.isChecked()) |
79 | | -//// options.isForbidSkipMultipleCrop(true) |
80 | | -//// options.setMaxScaleMultiplier(100f) |
81 | | -// |
82 | | -// if (selectorStyle != null && selectorStyle.getSelectMainStyle().getStatusBarColor() !== 0) { |
83 | | -// val mainStyle: SelectMainStyle = selectorStyle.getSelectMainStyle() |
84 | | -// val isDarkStatusBarBlack: Boolean = mainStyle.isDarkStatusBarBlack() |
85 | | -// val statusBarColor: Int = mainStyle.getStatusBarColor() |
86 | | -// options.isDarkStatusBarBlack(isDarkStatusBarBlack) |
87 | | -// if (StyleUtils.checkStyleValidity(statusBarColor)) { |
88 | | -// options.setStatusBarColor(statusBarColor) |
89 | | -// options.setToolbarColor(statusBarColor) |
90 | | -// } else { |
91 | | -// options.setStatusBarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey)) |
92 | | -// options.setToolbarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey)) |
93 | | -// } |
94 | | -// val titleBarStyle: TitleBarStyle = selectorStyle.getTitleBarStyle() |
95 | | -// if (StyleUtils.checkStyleValidity(titleBarStyle.getTitleTextColor())) { |
96 | | -// options.setToolbarWidgetColor(titleBarStyle.getTitleTextColor()) |
97 | | -// } else { |
98 | | -// options.setToolbarWidgetColor( |
99 | | -// ContextCompat.getColor( |
100 | | -// getContext(), |
101 | | -// R.color.ps_color_white |
102 | | -// ) |
103 | | -// ) |
104 | | -// } |
105 | | -// } else { |
106 | | -// options.setStatusBarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey)) |
107 | | -// options.setToolbarColor(ContextCompat.getColor(getContext(), R.color.ps_color_grey)) |
108 | | -// options.setToolbarWidgetColor(ContextCompat.getColor(getContext(), R.color.ps_color_white)) |
109 | | -// } |
110 | | -// return options |
111 | | -//} |
| 1 | +package com.reactnativemultipleimagepicker |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.graphics.Bitmap |
| 5 | +import android.net.Uri |
| 6 | +import android.widget.ImageView |
| 7 | +import androidx.fragment.app.Fragment |
| 8 | +import com.bumptech.glide.Glide |
| 9 | +import com.luck.picture.lib.config.PictureMimeType |
| 10 | +import com.luck.picture.lib.engine.CropEngine |
| 11 | +import com.luck.picture.lib.entity.LocalMedia |
| 12 | +import com.luck.picture.lib.utils.DateUtils |
| 13 | +import com.yalantis.ucrop.UCrop |
| 14 | +import com.yalantis.ucrop.UCropImageEngine |
| 15 | +import java.io.File |
| 16 | + |
| 17 | + |
| 18 | +class CropEngine( |
| 19 | + appContext: Context, |
| 20 | + cropOption: UCrop.Options |
| 21 | +) : CropEngine { |
| 22 | + private val context = appContext |
| 23 | + private val option: UCrop.Options = cropOption |
| 24 | + override fun onStartCrop( |
| 25 | + fragment: Fragment, currentLocalMedia: LocalMedia, |
| 26 | + dataSource: ArrayList<LocalMedia>, requestCode: Int |
| 27 | + ) { |
| 28 | + val currentCropPath = currentLocalMedia.availablePath |
| 29 | + val inputUri: Uri = |
| 30 | + if (PictureMimeType.isContent(currentCropPath) || PictureMimeType.isHasHttp( |
| 31 | + currentCropPath |
| 32 | + ) |
| 33 | + ) { |
| 34 | + Uri.parse(currentCropPath) |
| 35 | + } else { |
| 36 | + Uri.fromFile(File(currentCropPath)) |
| 37 | + } |
| 38 | + val fileName: String = DateUtils.getCreateFileName("CROP_") + ".jpg" |
| 39 | + val destinationUri = Uri.fromFile(File(getSandboxPath(context), fileName)) |
| 40 | + val dataCropSource: ArrayList<String> = ArrayList() |
| 41 | + for (i in 0 until dataSource.size) { |
| 42 | + val media = dataSource[i] |
| 43 | + dataCropSource.add(media.availablePath) |
| 44 | + } |
| 45 | + val uCrop = UCrop.of(inputUri, destinationUri, dataCropSource) |
| 46 | + uCrop.setImageEngine(object : UCropImageEngine { |
| 47 | + override fun loadImage(context: Context, url: String, imageView: ImageView) { |
| 48 | + Glide.with(context).load(url).into(imageView) |
| 49 | + } |
| 50 | + |
| 51 | + override fun loadImage( |
| 52 | + context: Context?, |
| 53 | + url: Uri?, |
| 54 | + maxWidth: Int, |
| 55 | + maxHeight: Int, |
| 56 | + call: UCropImageEngine.OnCallbackListener<Bitmap>? |
| 57 | + ) { |
| 58 | + TODO("Not yet implemented") |
| 59 | + } |
| 60 | + }) |
| 61 | + uCrop.withOptions(option) |
| 62 | + uCrop.start(fragment.requireActivity(), fragment, requestCode) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +fun getSandboxPath(context: Context): String { |
| 67 | + val externalFilesDir: File? = context.getExternalFilesDir("") |
| 68 | + val customFile = File(externalFilesDir?.absolutePath, "Sandbox") |
| 69 | + if (!customFile.exists()) { |
| 70 | + customFile.mkdirs() |
| 71 | + } |
| 72 | + return customFile.absolutePath + File.separator |
| 73 | +} |
| 74 | + |
| 75 | + |
0 commit comments