-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathP23.kt
72 lines (63 loc) · 2.43 KB
/
P23.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
package alpha.dex.dexsix
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
class CameraPicture : AppCompatActivity() {
lateinit var requestCamera: ActivityResultLauncher<String>
lateinit var imageCapture: ActivityResultLauncher<Intent>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera_picture)
var btnCamera = findViewById<Button>(R.id.btnCamera)
requestCamera = registerForActivityResult(ActivityResultContracts.RequestPermission())
{
if (it) {
Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_LONG).show()
cameraCall()
} else
Toast.makeText(applicationContext, "Permission Denied", Toast.LENGTH_LONG).show()
}
imageCapture =
registerForActivityResult(ActivityResultContracts.StartActivityForResult())
{
if (it.resultCode == Activity.RESULT_OK) {
handleCameraImage(it.data)
}
}
btnCamera.setOnClickListener {
callPermission()
}
}
fun cameraCall() {
val camera_intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
imageCapture.launch(camera_intent)
}
fun handleCameraImage(intent: Intent?) {
var imageCamera = findViewById<ImageView>(R.id.imageCamera)
val bitmap = intent?.extras?.get("data") as Bitmap
imageCamera.setImageBitmap(bitmap)
}
fun callPermission() {
when {
ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED -> {
Toast.makeText(applicationContext, "Permission Granted Already", Toast.LENGTH_LONG)
.show()
cameraCall()
}
else -> {
requestCamera.launch(android.Manifest.permission.CAMERA)
}
}
}
}