Skip to content

Commit

Permalink
returning bitmap from capture photo method
Browse files Browse the repository at this point in the history
  • Loading branch information
shahrukhahmed94 committed Dec 28, 2024
1 parent acbb793 commit 35f28b7
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions AICardDetector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ dependencies {
implementation("com.google.accompanist:accompanist-permissions:0.33.2-alpha")


// Kotlin Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import kotlinx.coroutines.withContext
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Locale
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlin.random.Random

object ObjectDetectionManagerFactory {
Expand Down Expand Up @@ -156,7 +159,7 @@ object ObjectDetectionManagerFactory {



fun capturePhoto(
/** fun capturePhoto(
context: Context,
// navController: NavController,
cameraController: LifecycleCameraController,
Expand Down Expand Up @@ -238,7 +241,83 @@ object ObjectDetectionManagerFactory {
}
}
)
}
}*/

suspend fun capturePhoto(
context: Context,
cameraController: LifecycleCameraController,
screenWidth: Float,
screenHeight: Float,
detections: List<Detection>
): Bitmap? = suspendCoroutine { continuation ->
cameraController.takePicture(
ContextCompat.getMainExecutor(context),
object : ImageCapture.OnImageCapturedCallback() {

override fun onCaptureSuccess(image: ImageProxy) {
super.onCaptureSuccess(image)
Log.d("TAG", "onCaptureSuccess() called for capturePhoto")

try {
val rotatedImageMatrix = Matrix().apply {
postRotate(image.imageInfo.rotationDegrees.toFloat())
}

// Convert the captured image to a Bitmap
val rotatedBitmap = Bitmap.createBitmap(
image.toBitmap(),
0,
0,
image.width,
image.height,
rotatedImageMatrix,
true
)

val detection = detections.firstOrNull()
if (detection != null) {
val croppedBitmap = cropImage(
rotatedBitmap,
detection.boundingBox,
detection.tensorImageWidth,
detection.tensorImageHeight
)

// Save cropped bitmap asynchronously
CoroutineScope(Dispatchers.IO).launch {
try {
saveBitmapToDevice(context, croppedBitmap)
withContext(Dispatchers.Main) {
continuation.resume(croppedBitmap)
}
} catch (e: Exception) {
Log.e("TAG", "Failed to save or process the image: $e")
withContext(Dispatchers.Main) {
continuation.resumeWithException(e)
}
}
}
} else {
Log.e("TAG", "No detection found.")
continuation.resume(null)
}
} catch (e: Exception) {
Log.e("TAG", "Exception in onCaptureSuccess: $e")
continuation.resumeWithException(e)
} finally {
image.close()
}
}

override fun onError(exception: ImageCaptureException) {
super.onError(exception)
Log.e("TAG", "onError() called for capturePhoto with: exception = $exception")
continuation.resumeWithException(exception)
}
}
)
}




Expand Down
41 changes: 40 additions & 1 deletion app/src/main/java/com/shahrukh/aicarddetector/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ import com.shahrukh.aicarddetector.presentation.utils.Constants
import com.shahrukh.aicarddetector.presentation.utils.Dimens
import com.shahrukh.aicarddetector.presentation.utils.ImageScalingUtils
import com.shahrukh.aicarddetector.ui.theme.AICardDetectorTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -146,7 +149,7 @@ fun CardDetectorScreen() {
.padding(top = Dimens.Padding8dp),
verticalArrangement = Arrangement.SpaceAround
) {
ImageButton(
/** ImageButton(
drawableResourceId = R.drawable.ic_capture,
contentDescriptionResourceId = R.string.capture_button_description,
modifier = Modifier
Expand All @@ -170,9 +173,45 @@ fun CardDetectorScreen() {
// Show toast of Save State
}
)*/
ImageButton(
drawableResourceId = R.drawable.ic_capture,
contentDescriptionResourceId = R.string.capture_button_description,
modifier = Modifier
.size(Dimens.CaptureButtonSize)
.clip(CircleShape)
.align(Alignment.CenterHorizontally)
.clickable {
// Ensure you have a CoroutineScope (e.g., lifecycleScope or a ViewModel scope)
cameraController?.let {
CoroutineScope(Dispatchers.Main).launch {
val croppedBitmap = ObjectDetectionManagerFactory.capturePhoto(
context = context,
cameraController = it,
screenWidth = screenWidth,
screenHeight = screenHeight,
detections = detections.value
)

croppedBitmap?.let { bitmap ->
// Use the cropped bitmap here, for example:
// Show it in a UI
// Save it to the device
Log.d("BitmapInfo", "Width: ${bitmap.width}, Height: ${bitmap.height}")
Log.d("BitmapInfo", "Config: ${bitmap.config}")
Log.d("BitmapInfo", "Byte Count: ${bitmap.allocationByteCount}")

Toast.makeText(context, "Photo captured successfully!", Toast.LENGTH_SHORT).show()
} ?: run {
Toast.makeText(context, "Failed to capture photo.", Toast.LENGTH_SHORT).show()
}
}
}
}
)



}
}

Expand Down

0 comments on commit 35f28b7

Please sign in to comment.