Skip to content

Commit

Permalink
Drjacky#87 Support crop selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ZahraHeydari committed May 19, 2023
1 parent e36fc8f commit f586c94
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,48 +277,49 @@ open class ImagePicker {
fun createIntent(): Intent =
Intent(activity, ImagePickerActivity::class.java).apply { putExtras(getBundle()) }

fun createIntentFromDialog(
onResult: (Intent) -> Unit
) {
fun createIntentFromDialog(onResult: (Intent) -> Unit) {
if (imageProvider == ImageProvider.BOTH) {
DialogHelper.showChooseAppDialog(
context = activity,
listener = object : ResultListener<ImageProvider> {
context = activity, listener = object : ResultListener<ImageProvider> {
override fun onResult(t: ImageProvider?) {
t?.let {
imageProvider = it
setImageProviderInterceptor {
setCrop(imageProvider)
}
imageProviderInterceptor?.invoke(imageProvider)
onResult(createIntent())
}
}
},
dismissListener
}, dismissListener
)
}
}

private fun setCrop(imageProvider: ImageProvider) {
crop = imageProvider == ImageProvider.CAMERA_WITH_CROP
|| imageProvider == ImageProvider.GALLERY_WITH_CROP
}

/**
* Get Bundle for ImagePickerActivity
*/
private fun getBundle(): Bundle {
return Bundle().apply {
putSerializable(EXTRA_IMAGE_PROVIDER, imageProvider)
putStringArray(EXTRA_MIME_TYPES, mimeTypes)

putBoolean(EXTRA_CROP_OVAL, cropOval)
putBoolean(EXTRA_CROP_FREE_STYLE, cropFreeStyle)
putBoolean(EXTRA_CROP, crop)
putBoolean(MULTIPLE_FILES_ALLOWED, isMultiple)
putFloat(EXTRA_CROP_X, cropX)
putFloat(EXTRA_CROP_Y, cropY)
putSerializable(EXTRA_OUTPUT_FORMAT, outputFormat)

putInt(EXTRA_MAX_WIDTH, maxWidth)
putInt(EXTRA_MAX_HEIGHT, maxHeight)

putBoolean(EXTRA_KEEP_RATIO, keepRatio)
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,11 @@ class ImagePickerActivity : AppCompatActivity() {

// Create Gallery/Camera Provider
when (intent?.getSerializableExtra(ImagePicker.EXTRA_IMAGE_PROVIDER) as ImageProvider?) {
ImageProvider.GALLERY -> {
mGalleryProvider = GalleryProvider(this) { galleryLauncher.launch(it) }
// Pick Gallery Image
savedInstanceState ?: mGalleryProvider?.startIntent()
}
ImageProvider.CAMERA -> {
mCameraProvider = CameraProvider(this, false) { cameraLauncher.launch(it) }
mCameraProvider?.onRestoreInstanceState(savedInstanceState)
// Pick Camera Image
savedInstanceState ?: mCameraProvider?.startIntent()
}
ImageProvider.FRONT_CAMERA -> {
mCameraProvider = CameraProvider(this, true) { cameraLauncher.launch(it) }
mCameraProvider?.onRestoreInstanceState(savedInstanceState)
// Try Pick Front Camera Image
savedInstanceState ?: mCameraProvider?.startIntent()
}
ImageProvider.GALLERY -> pickGalleryImage(savedInstanceState)
ImageProvider.GALLERY_WITH_CROP -> pickGalleryImage(savedInstanceState)
ImageProvider.CAMERA -> pickCameraImage(savedInstanceState, false)
ImageProvider.CAMERA_WITH_CROP -> pickCameraImage(savedInstanceState, false)
ImageProvider.FRONT_CAMERA -> pickCameraImage(savedInstanceState, true)
else -> {
// Something went Wrong! This case should never happen
Log.e(TAG, "Image provider can not be null")
Expand All @@ -139,6 +127,19 @@ class ImagePickerActivity : AppCompatActivity() {
}
}

private fun pickGalleryImage(savedInstanceState: Bundle?) {
mGalleryProvider = GalleryProvider(this) { galleryLauncher.launch(it) }
// Pick Gallery Image
savedInstanceState ?: mGalleryProvider?.startIntent()
}

private fun pickCameraImage(savedInstanceState: Bundle?, tryFrontCamera: Boolean) {
mCameraProvider = CameraProvider(this, tryFrontCamera) { cameraLauncher.launch(it) }
mCameraProvider?.onRestoreInstanceState(savedInstanceState)
// Pick Camera Image
savedInstanceState ?: mCameraProvider?.startIntent()
}

/**
* Dispatch incoming result to the correct provider.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ package com.github.drjacky.imagepicker.constant
*/
enum class ImageProvider {
GALLERY,
GALLERY_WITH_CROP,
CAMERA,
CAMERA_WITH_CROP,
FRONT_CAMERA,
BOTH
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ internal object DialogHelper {

// Handle Camera option click
customView.lytCameraPick.setOnClickListener {
listener.onResult(ImageProvider.CAMERA)
listener.onResult(
if (customView.displayCropCheckBox.isChecked) ImageProvider.CAMERA_WITH_CROP else ImageProvider.CAMERA
)
dialog.dismiss()
}

// Handle Gallery option click
customView.lytGalleryPick.setOnClickListener {
listener.onResult(ImageProvider.GALLERY)
listener.onResult(
if (customView.displayCropCheckBox.isChecked) ImageProvider.GALLERY_WITH_CROP else ImageProvider.GALLERY
)
dialog.dismiss()
}
}
}
}
101 changes: 58 additions & 43 deletions imagepicker/src/main/res/layout/dialog_choose_app.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,73 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal"
android:orientation="vertical"
android:padding="16dp">

<LinearLayout
android:id="@+id/lytCameraPick"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/ic_photo_camera_black_48dp"
app:tint="@color/dialog_choose_icon_color" />

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/lytCameraPick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/title_camera"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/dialog_choose_text_color" />
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/ic_photo_camera_black_48dp"
app:tint="@color/dialog_choose_icon_color" />

<LinearLayout
android:id="@+id/lytGalleryPick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/ic_photo_black_48dp"
app:tint="@color/dialog_choose_icon_color" />

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/title_camera"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/dialog_choose_text_color" />

</LinearLayout>

<LinearLayout
android:id="@+id/lytGalleryPick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/title_gallery"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/dialog_choose_text_color" />
android:layout_marginTop="10dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">

<androidx.appcompat.widget.AppCompatImageView
android:layout_width="48dp"
android:layout_height="48dp"
app:srcCompat="@drawable/ic_photo_black_48dp"
app:tint="@color/dialog_choose_icon_color" />

<androidx.appcompat.widget.AppCompatTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="@string/title_gallery"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textColor="@color/dialog_choose_text_color" />

</LinearLayout>

</LinearLayout>

<CheckBox
android:id="@+id/displayCropCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:text="@string/display_crop" />

</LinearLayout>
1 change: 1 addition & 0 deletions imagepicker/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<string name="action_cancel">Cancel</string>
<string name="title_gallery">Gallery</string>
<string name="title_camera">Camera</string>
<string name="display_crop">Display crop</string>

<string name="permission_gallery_denied">
<![CDATA[Storage permission is needed to pick gallery image. Please allow storage permission from Settings > Permissions.]]></string>
Expand Down

0 comments on commit f586c94

Please sign in to comment.