Skip to content

Commit

Permalink
Merge pull request #59 from kreativityapps/fix_npe
Browse files Browse the repository at this point in the history
Fix bitmap exception crash
  • Loading branch information
jachzen authored Apr 8, 2024
2 parents c4dd7be + c6218d4 commit f01824a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
// check for errors
val error = data?.extras?.getString("error")
if (error != null) {
throw Exception("error - $error")
pendingResult?.error("ERROR", "error - $error", null)
return@ActivityResultListener true
}

// get an array with scanned document file paths
Expand All @@ -92,33 +93,34 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}?.toList()

// trigger the success event handler with an array of cropped images
this.pendingResult?.success(successResponse)
pendingResult?.success(successResponse)
return@ActivityResultListener true
}

Activity.RESULT_CANCELED -> {
// user closed camera
this.pendingResult?.success(emptyList<String>())
pendingResult?.success(emptyList<String>())
return@ActivityResultListener true
}

else -> {
return@ActivityResultListener false
}
else -> return@ActivityResultListener false
}
} else {
when (resultCode) {
Activity.RESULT_OK -> {
// check for errors
val error = data?.extras?.getString("error")
if (error != null) {
throw Exception("error - $error")
pendingResult?.error("ERROR", "error - $error", null)
return@ActivityResultListener true
}

// get an array with scanned document file paths
val croppedImageResults =
data?.getStringArrayListExtra("croppedImageResults")?.toList()
?: throw Exception("No cropped images returned")
?: let {
pendingResult?.error("ERROR", "No cropped images returned", null)
return@ActivityResultListener true
}

// return a list of file paths
// removing file uri prefix as Flutter file will have problems with it
Expand All @@ -127,19 +129,16 @@ class CunningDocumentScannerPlugin : FlutterPlugin, MethodCallHandler, ActivityA
}.toList()

// trigger the success event handler with an array of cropped images
this.pendingResult?.success(successResponse)
pendingResult?.success(successResponse)
return@ActivityResultListener true
}

Activity.RESULT_CANCELED -> {
// user closed camera
this.pendingResult?.success(emptyList<String>())
pendingResult?.success(emptyList<String>())
return@ActivityResultListener true
}

else -> {
return@ActivityResultListener false
}
else -> return@ActivityResultListener false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ class DocumentScannerActivity : AppCompatActivity() {
}

// get bitmap from photo file path
val photo: Bitmap = ImageUtil().getImageFromFilePath(originalPhotoPath)
val photo: Bitmap? = try {
ImageUtil().getImageFromFilePath(originalPhotoPath)
} catch (exception: Exception) {
finishIntentWithError("Unable to get bitmap: ${exception.localizedMessage}")
return@CameraUtil
}

if (photo == null) {
finishIntentWithError("Document bitmap is null.")
return@CameraUtil
}

// get document corners by detecting them, or falling back to photo corners with
// slight margin if we can't find the corners
Expand Down Expand Up @@ -313,7 +323,7 @@ class DocumentScannerActivity : AppCompatActivity() {
val croppedImageResults = arrayListOf<String>()
for ((pageNumber, document) in documents.withIndex()) {
// crop document photo by using corners
val croppedImage: Bitmap = try {
val croppedImage: Bitmap? = try {
ImageUtil().crop(
document.originalPhotoFilePath,
document.corners
Expand All @@ -323,6 +333,11 @@ class DocumentScannerActivity : AppCompatActivity() {
return
}

if (croppedImage == null) {
finishIntentWithError("Result of cropping is null")
return
}

// delete original document photo
File(document.originalPhotoFilePath).delete()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import kotlin.math.sqrt

class ImageUtil {

fun getImageFromFilePath(filePath: String): Bitmap {
fun getImageFromFilePath(filePath: String): Bitmap? {
val rotation = getRotationDegrees(filePath)
var bitmap = BitmapFactory.decodeFile(filePath)
val bitmap = BitmapFactory.decodeFile(filePath) ?: return null

// Correct image rotation
if (rotation != 0) {
return if (rotation != 0) {
val matrix = Matrix().apply { postRotate(rotation.toFloat()) }
bitmap = Bitmap.createBitmap(bitmap!!, 0, 0, bitmap.width, bitmap.height, matrix, true)
Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, matrix, true)
} else {
bitmap
}
return bitmap!!
}

private fun getRotationDegrees(filePath: String): Int {
Expand All @@ -41,8 +41,8 @@ class ImageUtil {
}


fun crop(photoFilePath: String, corners: Quad): Bitmap {
val bitmap = getImageFromFilePath(photoFilePath)
fun crop(photoFilePath: String, corners: Quad): Bitmap? {
val bitmap = getImageFromFilePath(photoFilePath) ?: return null

// Convert Quad corners to a float array manually
val src = floatArrayOf(
Expand Down

0 comments on commit f01824a

Please sign in to comment.