Skip to content

Commit b3244ef

Browse files
committed
✨ feat: add camera config, remove allowedCamera props
1 parent f6a44ab commit b3244ef

34 files changed

+1246
-691
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.margelo.nitro.multipleimagepicker
2+
3+
import android.content.Context
4+
import androidx.fragment.app.Fragment
5+
import com.bumptech.glide.Glide
6+
import com.luck.lib.camerax.SimpleCameraX
7+
import com.luck.picture.lib.interfaces.OnCameraInterceptListener
8+
import java.io.File
9+
10+
class CameraEngine(private val appContext: Context, val config: PickerCameraConfig) : OnCameraInterceptListener {
11+
override fun openCamera(fragment: Fragment, cameraMode: Int, requestCode: Int) {
12+
val camera = SimpleCameraX.of()
13+
camera.isAutoRotation(true)
14+
camera.setCameraMode(cameraMode)
15+
camera.isDisplayRecordChangeTime(true)
16+
camera.isManualFocusCameraPreview(true)
17+
camera.isZoomCameraPreview(true)
18+
camera.setOutputPathDir(getSandboxCameraOutputPath())
19+
camera.setRecordVideoMaxSecond(config.videoMaximumDuration?.toInt() ?: 60)
20+
21+
// camera.setPermissionDeniedListener(getSimpleXPermissionDeniedListener())
22+
// camera.setPermissionDescriptionListener(getSimpleXPermissionDescriptionListener())
23+
camera.setImageEngine { context, url, imageView ->
24+
Glide.with(context).load(url).into(imageView)
25+
}
26+
camera.start(fragment.requireActivity(), fragment, requestCode)
27+
}
28+
29+
private fun getSandboxCameraOutputPath(): String {
30+
val externalFilesDir: File? = appContext.getExternalFilesDir("")
31+
val customFile = File(externalFilesDir?.absolutePath, "Sandbox")
32+
if (!customFile.exists()) {
33+
customFile.mkdirs()
34+
}
35+
return customFile.absolutePath + File.separator
36+
37+
}
38+
}

android/src/main/java/com/margelo/nitro/multipleimagepicker/CompressEngine.kt

-43
This file was deleted.

android/src/main/java/com/margelo/nitro/multipleimagepicker/GlideEngine.kt

+47-74
Original file line numberDiff line numberDiff line change
@@ -8,105 +8,78 @@ import com.bumptech.glide.load.resource.bitmap.RoundedCorners
88
import com.luck.picture.lib.engine.ImageEngine
99
import com.luck.picture.lib.utils.ActivityCompatHelper
1010

11-
12-
/**
13-
* @author:luck
14-
* @date:2019-11-13 17:02
15-
* @describe:Glide加载引擎
16-
*/
1711
class GlideEngine private constructor() : ImageEngine {
18-
/**
19-
* 加载图片
20-
*
21-
* @param context 上下文
22-
* @param url 资源url
23-
* @param imageView 图片承载控件
24-
*/
25-
override fun loadImage(context: Context, url: String, imageView: ImageView) {
26-
if (!ActivityCompatHelper.assertValidRequest(context)) {
27-
return
28-
}
29-
Glide.with(context)
12+
override fun loadImage(context: Context, url: String, imageView: ImageView) {
13+
if (!ActivityCompatHelper.assertValidRequest(context)) {
14+
return
15+
}
16+
Glide.with(context)
3017
.load(url)
3118
.into(imageView)
32-
}
33-
34-
override fun loadImage(
35-
context: Context,
36-
imageView: ImageView,
37-
url: String,
38-
maxWidth: Int,
39-
maxHeight: Int
40-
) {
41-
if (!ActivityCompatHelper.assertValidRequest(context)) {
42-
return
4319
}
44-
Glide.with(context)
20+
21+
override fun loadImage(
22+
context: Context,
23+
imageView: ImageView,
24+
url: String,
25+
maxWidth: Int,
26+
maxHeight: Int
27+
) {
28+
if (!ActivityCompatHelper.assertValidRequest(context)) {
29+
return
30+
}
31+
Glide.with(context)
4532
.load(url)
4633
.override(maxWidth, maxHeight)
4734
.into(imageView)
48-
}
49-
50-
/**
51-
* 加载相册目录封面
52-
*
53-
* @param context 上下文
54-
* @param url 图片路径
55-
* @param imageView 承载图片ImageView
56-
*/
57-
override fun loadAlbumCover(context: Context, url: String, imageView: ImageView) {
58-
if (!ActivityCompatHelper.assertValidRequest(context)) {
59-
return
6035
}
61-
Glide.with(context)
36+
37+
override fun loadAlbumCover(context: Context, url: String, imageView: ImageView) {
38+
if (!ActivityCompatHelper.assertValidRequest(context)) {
39+
return
40+
}
41+
Glide.with(context)
6242
.asBitmap()
6343
.load(url)
6444
.override(180, 180)
6545
.sizeMultiplier(0.5f)
6646
.transform(CenterCrop(), RoundedCorners(8))
6747
.into(imageView)
68-
}
69-
70-
/**
71-
* 加载图片列表图片
72-
*
73-
* @param context 上下文
74-
* @param url 图片路径
75-
* @param imageView 承载图片ImageView
76-
*/
77-
override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
78-
if (!ActivityCompatHelper.assertValidRequest(context)) {
79-
return
8048
}
81-
Glide.with(context)
49+
50+
override fun loadGridImage(context: Context, url: String, imageView: ImageView) {
51+
if (!ActivityCompatHelper.assertValidRequest(context)) {
52+
return
53+
}
54+
Glide.with(context)
8255
.load(url)
8356
.override(200, 200)
8457
.centerCrop()
8558
.placeholder(com.luck.picture.lib.R.drawable.ps_image_placeholder)
8659
.into(imageView)
87-
}
60+
}
8861

89-
override fun pauseRequests(context: Context) {
90-
if (!ActivityCompatHelper.assertValidRequest(context)) {
91-
return
62+
override fun pauseRequests(context: Context) {
63+
if (!ActivityCompatHelper.assertValidRequest(context)) {
64+
return
65+
}
66+
Glide.with(context).pauseRequests()
9267
}
93-
Glide.with(context).pauseRequests()
94-
}
9568

96-
override fun resumeRequests(context: Context) {
97-
if (!ActivityCompatHelper.assertValidRequest(context)) {
98-
return
69+
override fun resumeRequests(context: Context) {
70+
if (!ActivityCompatHelper.assertValidRequest(context)) {
71+
return
72+
}
73+
Glide.with(context).resumeRequests()
9974
}
100-
Glide.with(context).resumeRequests()
101-
}
10275

103-
private object InstanceHolder {
104-
val instance = GlideEngine()
105-
}
76+
private object InstanceHolder {
77+
val instance = GlideEngine()
78+
}
10679

107-
companion object {
108-
fun createGlideEngine(): GlideEngine {
109-
return InstanceHolder.instance
80+
companion object {
81+
fun createGlideEngine(): GlideEngine {
82+
return InstanceHolder.instance
83+
}
11084
}
111-
}
11285
}

android/src/main/java/com/margelo/nitro/multipleimagepicker/MultipleImagePickerImp.kt

+8-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
9696
.setImageEngine(imageEngine)
9797
.setSelectedData(dataList)
9898
.setSelectorUIStyle(style)
99+
99100
.apply {
100101
if (isCrop) {
101102
setCropOption(config.crop)
@@ -114,6 +115,13 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
114115
maxFileSize?.let {
115116
setFilterMaxFileSize(it)
116117
}
118+
119+
120+
isDisplayCamera(config.camera != null)
121+
122+
config.camera?.let {
123+
setCameraInterceptListener(CameraEngine(appContext, it))
124+
}
117125
}
118126
.setImageSpanCount(config.numberOfColumn?.toInt() ?: 3)
119127
.setMaxSelectNum(maxSelect)
@@ -129,8 +137,6 @@ class MultipleImagePickerImp(reactContext: ReactApplicationContext?) :
129137
// isPreview
130138
.isPreviewImage(isPreview)
131139
.isPreviewVideo(isPreview)
132-
//
133-
.isDisplayCamera(config.allowedCamera ?: true)
134140
.isDisplayTimeAxis(true)
135141
.setSelectionMode(selectMode)
136142
.isOriginalControl(config.isHiddenOriginalButton == false)

docs/docs/CONFIG.mdx

+30-9
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ See [**Result**](/result)
2727
- **Default**: `[]`
2828
- **Required**: No
2929

30-
### `allowedCamera`
31-
32-
Enable camera functionality.
33-
34-
- **Type**: boolean
35-
- **Default**: `true`
36-
- **Required**: No
37-
- **Platform**: iOS, Android
38-
3930
### `allowedLimit`
4031

4132
Display additional select more media when permission on `iOS` is limited.
@@ -81,6 +72,36 @@ Maximum number of videos allowed.
8172
- **Required**: No
8273
- **Platform**: iOS, Android
8374

75+
## Camera 📸
76+
77+
Configuration camera functionality.
78+
79+
- **Type**: object
80+
- **Default**: `{}`
81+
- **Required**: No
82+
- **Platform**: iOS, Android
83+
84+
### `cameraDevice`
85+
86+
Camera device to be used.
87+
88+
- **Type**: `string`
89+
- **Default**: `back`
90+
- **Required**: No
91+
- **Platform**: iOS
92+
- **Options**:
93+
- `back`: Back camera
94+
- `front`: Front camera
95+
96+
### `videoMaximumDuration`
97+
98+
Maximum video duration in seconds.
99+
100+
- **Type**: number
101+
- **Default**: 60
102+
- **Required**: No
103+
- **Platform**: iOS, Android
104+
84105
## Crop 🪚
85106

86107
Configuration for image cropping functionality.

docs/docs/index.md

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ React Native Multiple Image Picker **(RNMIP)** enables application to pick image
3131
| 🎨 | UI Customization (numberOfColumn, spacing, primaryColor ... ) |
3232
| 🌚 | Dark Mode, Light Mode |
3333
| 🌄 | Choose multiple images/video. |
34-
| 🤐 | Compress image after selected (new) ✨ |
3534
| 📦 | Support smart album `(camera roll, selfies, panoramas, favorites, videos...)`. |
3635
| 📺 | Display video duration. |
3736
| 🎆 | Preview image/video. |

example/src/index.tsx

+32-13
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ export default function App() {
8383
const response = await openPicker({
8484
...options,
8585
selectedAssets: images,
86-
crop: {
87-
ratio: [
88-
{ title: 'Instagram', width: 1, height: 1 },
89-
{ title: 'Twitter', width: 16, height: 9 },
90-
{ title: 'Facebook', width: 12, height: 11 },
91-
],
92-
},
9386
})
9487

9588
setImages(Array.isArray(response) ? response : [response])
@@ -262,13 +255,39 @@ export default function App() {
262255
/>
263256
) : null}
264257

265-
{/* allowedCamera */}
258+
<Text style={style.title}>Camera 📸</Text>
259+
<View style={style.section}>
260+
<SectionView
261+
title="camera"
262+
description="Enable camera functionality."
263+
>
264+
<Switch
265+
value={options.camera !== undefined}
266+
onValueChange={(value) =>
267+
setOptions('camera', value ? {} : undefined)
268+
}
269+
/>
270+
</SectionView>
266271

267-
<SectionView
268-
title="allowedCamera"
269-
description="Enable camera functionality."
270-
optionKey="allowedCamera"
271-
/>
272+
{/* camera videoMaximumDuration */}
273+
<SectionView
274+
title={'camera.videoMaximumDuration' as any}
275+
description="The maximum duration of video that can be selected."
276+
>
277+
<Input
278+
value={
279+
options.camera?.videoMaximumDuration?.toString() ?? ''
280+
}
281+
placeholder="Video Duration"
282+
onChangeText={(value) => {
283+
setOptions('camera', {
284+
...(options.camera ?? { cameraDevice: 'back' }),
285+
videoMaximumDuration: parseNumber(value),
286+
})
287+
}}
288+
/>
289+
</SectionView>
290+
</View>
272291

273292
{IS_IOS ? (
274293
<>

0 commit comments

Comments
 (0)