Skip to content

Commit

Permalink
width might not be initially available
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarcs committed Nov 4, 2022
1 parent 96fdc6c commit 92baa33
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class DocumentScannerActivity : AppCompatActivity() {
// user is allowed to move corners to make corrections
try {
// set preview image height based off of photo dimensions
imageView.setImagePreviewHeight(photo, screenWidth, screenHeight)
imageView.setImagePreviewBounds(photo, screenWidth, screenHeight)

// display original photo, so user can adjust detected corners
imageView.setImageBitmap(photo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ val AppCompatActivity.screenBounds: Rect get() {
}

// fall back to get screen width and height if using a version before Android R
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
return Rect(0, 0 , displayMetrics.widthPixels, displayMetrics.heightPixels)
return Rect(
0, 0 , windowManager.defaultDisplay.width, windowManager.defaultDisplay.height
)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ class ImageCropView(context: Context, attrs: AttributeSet) : AppCompatImageView(
*/
private var imagePreviewHeight = height

/**
* @property imagePreviewWidth this is needed because width doesn't update immediately
* after we set the image
*/
private var imagePreviewWidth = width

/**
* @property ratio image container height to image height ratio used to map container
* to image coordinates and vice versa
Expand Down Expand Up @@ -84,7 +90,7 @@ class ImageCropView(context: Context, attrs: AttributeSet) : AppCompatImageView(
* @param photo the original photo with a rectangular document
* @param screenWidth the device width
*/
fun setImagePreviewHeight(photo: Bitmap, screenWidth: Int, screenHeight: Int) {
fun setImagePreviewBounds(photo: Bitmap, screenWidth: Int, screenHeight: Int) {
// image width to height aspect ratio
val imageRatio = photo.width.toFloat() / photo.height.toFloat()
val buttonsViewMinHeight = context.resources.getDimension(
Expand All @@ -105,8 +111,11 @@ class ImageCropView(context: Context, attrs: AttributeSet) : AppCompatImageView(
screenHeight - buttonsViewMinHeight
)

// image container initially has a 0 height, once we calculate the height we can set it
imagePreviewWidth = screenWidth

// image container initially has a 0 width and 0 height, calculate both and set them
layoutParams.height = imagePreviewHeight
layoutParams.width = imagePreviewWidth

// refresh layout after we change height
requestLayout()
Expand Down Expand Up @@ -134,26 +143,26 @@ class ImageCropView(context: Context, attrs: AttributeSet) : AppCompatImageView(
private val imagePreviewBounds: RectF
get() {
// image container width to height ratio
val imageViewRatio: Float = width.toFloat() / imagePreviewHeight.toFloat()
val imageViewRatio: Float = imagePreviewWidth.toFloat() / imagePreviewHeight.toFloat()

// image width to height ratio
val imageRatio = drawable.intrinsicWidth.toFloat() / drawable.intrinsicHeight.toFloat()

var left = 0f
var top = 0f
var right = width.toFloat()
var right = imagePreviewWidth.toFloat()
var bottom = imagePreviewHeight.toFloat()

if (imageRatio > imageViewRatio) {
// if the image is really wide, there's blank space at the top and bottom
val offset = (imagePreviewHeight - (width / imageRatio)) / 2
val offset = (imagePreviewHeight - (imagePreviewWidth / imageRatio)) / 2
top += offset
bottom -= offset
} else {
// if the image is really tall, there's blank space at the left and right
// it's also possible that the image ratio matches the image container ratio
// in which case there's no blank space
val offset = (width - (imagePreviewHeight * imageRatio)) / 2
val offset = (imagePreviewWidth - (imagePreviewHeight * imageRatio)) / 2
left += offset
right -= offset
}
Expand Down

0 comments on commit 92baa33

Please sign in to comment.