Repository navigation
Expand file tree
/
Copy pathSampleCropFragment.kt
More file actions
147 lines (130 loc) · 4.68 KB
/
Copy pathSampleCropFragment.kt
File metadata and controls
147 lines (130 loc) · 4.68 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
@file:Suppress("DEPRECATION")
package com.canhub.cropper.sample
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.FileProvider
import androidx.fragment.app.Fragment
import com.canhub.cropper.CropImage
import com.canhub.cropper.CropImageContract
import com.canhub.cropper.CropImageContractOptions
import com.canhub.cropper.CropImageOptions
import com.example.croppersample.databinding.FragmentCameraBinding
import timber.log.Timber
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
internal class SampleCropFragment : Fragment() {
private var _binding: FragmentCameraBinding? = null
private val binding get() = _binding!!
private var outputUri: Uri? = null
private val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) {
if (it) {
startCameraWithUri()
} else {
showErrorMessage("taking picture failed")
}
}
private val cropImage = registerForActivityResult(CropImageContract()) { result ->
when {
result.isSuccessful -> {
Timber.tag("AIC-Sample").i("Original bitmap: ${result.originalBitmap}")
Timber.tag("AIC-Sample").i("Original uri: ${result.originalUri}")
Timber.tag("AIC-Sample").i("Output bitmap: ${result.bitmap}")
Timber.tag("AIC-Sample").i("Output uri: ${result.getUriFilePath(requireContext())}")
handleCropImageResult(result.uriContent.toString())
}
result is CropImage.CancelledResult -> showErrorMessage("cropping image was cancelled by the user")
else -> showErrorMessage("cropping image failed")
}
}
private val customCropImage = registerForActivityResult(CropImageContract()) {
if (it !is CropImage.CancelledResult) {
handleCropImageResult(it.uriContent.toString())
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = FragmentCameraBinding.inflate(layoutInflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.takePictureBeforeCallLibraryWithUri.setOnClickListener {
setupOutputUri()
takePicture.launch(outputUri)
}
binding.callLibraryWithoutUri.setOnClickListener {
startCameraWithoutUri(includeCamera = true, includeGallery = true)
}
binding.callLibraryWithoutUriCameraOnly.setOnClickListener {
startCameraWithoutUri(includeCamera = true, includeGallery = false)
}
binding.callLibraryWithoutUriGalleryOnly.setOnClickListener {
startCameraWithoutUri(includeCamera = false, includeGallery = true)
}
}
private fun startCameraWithoutUri(includeCamera: Boolean, includeGallery: Boolean) {
customCropImage.launch(
CropImageContractOptions(
uri = null,
cropImageOptions = CropImageOptions(
imageSourceIncludeCamera = includeCamera,
imageSourceIncludeGallery = includeGallery,
),
),
)
}
private fun startCameraWithUri() {
cropImage.launch(
CropImageContractOptions(
uri = outputUri,
cropImageOptions = CropImageOptions(),
),
)
}
private fun showErrorMessage(message: String) {
Timber.tag("AIC-Sample").e("Camera error: $message")
Toast.makeText(activity, "Crop failed: $message", Toast.LENGTH_SHORT).show()
}
private fun handleCropImageResult(uri: String) {
SampleResultScreen.start(this, null, Uri.parse(uri.replace("file:", "")), null)
}
private fun setupOutputUri() {
if (outputUri == null) {
context?.let { ctx ->
val authorities = "${ctx.applicationContext?.packageName}$AUTHORITY_SUFFIX"
outputUri = FileProvider.getUriForFile(ctx, authorities, createImageFile())
}
}
}
private fun createImageFile(): File {
val timeStamp = SimpleDateFormat(DATE_FORMAT, Locale.getDefault()).format(Date())
val storageDir: File? = activity?.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"$FILE_NAMING_PREFIX${timeStamp}$FILE_NAMING_SUFFIX",
FILE_FORMAT,
storageDir,
)
}
companion object {
const val DATE_FORMAT = "yyyyMMdd_HHmmss"
const val FILE_NAMING_PREFIX = "JPEG_"
const val FILE_NAMING_SUFFIX = "_"
const val FILE_FORMAT = ".jpg"
const val AUTHORITY_SUFFIX = ".cropper.fileprovider"
}
}