lyrebirdstudio / Croppy

Image Cropping Library for Android
1.13k stars 83 forks source link

Rotation image button #14

Open dan085 opened 4 years ago

dan085 commented 4 years ago

Its posible to incorporate button rotate image?

dan085 commented 4 years ago

I add this and not work whe push apply

 binding.floatingActionButton.setOnClickListener{

            if (exifRotation == 0) {
                exifRotation = 90
            } else if (exifRotation == 90) {
                exifRotation = 180
            } else if (exifRotation == 180) {
                exifRotation = 270
            } else if (exifRotation == 270) {
                exifRotation = 0
            }

            binding.cropView.rotation=exifRotation.toFloat()

            binding.cropView.setAspectRatio(AspectRatio.ASPECT_INS_1_1)
            viewModel.onAspectRatioChanged(AspectRatio.ASPECT_INS_1_1)

        }
dan085 commented 4 years ago

I add the code for the option rotation in result

  private var exifRotation: Float = 0f

  binding.imageBtnRotation.setOnClickListener{

            if (exifRotation == 0f) {
                exifRotation = 90f
            } else if (exifRotation == 90f) {
                exifRotation = 180f
            } else if (exifRotation == 180f) {
                exifRotation = 270f
            } else if (exifRotation == 270f) {
                exifRotation = 0f
            }

            binding.cropView.rotation=exifRotation
            binding.cropView.setAspectRatio(AspectRatio.ASPECT_INS_1_1)
            viewModel.onAspectRatioChanged(AspectRatio.ASPECT_INS_1_1)

        }

    fun getCroppedData(exifRotation:Float): CroppedBitmapData {
        val croppedBitmapRect = getCropSizeOriginal()

        if (bitmapRect.intersect(croppedBitmapRect).not()) {
            return CroppedBitmapData(croppedBitmap = bitmap)
        }

        val cropLeft = if (croppedBitmapRect.left.roundToInt() < bitmapRect.left) {
            bitmapRect.left.toInt()
        } else {
            croppedBitmapRect.left.roundToInt()
        }

        val cropTop = if (croppedBitmapRect.top.roundToInt() < bitmapRect.top) {
            bitmapRect.top.toInt()
        } else {
            croppedBitmapRect.top.roundToInt()
        }

        val cropRight = if (croppedBitmapRect.right.roundToInt() > bitmapRect.right) {
            bitmapRect.right.toInt()
        } else {
            croppedBitmapRect.right.roundToInt()
        }

        val cropBottom = if (croppedBitmapRect.bottom.roundToInt() > bitmapRect.bottom) {
            bitmapRect.bottom.toInt()
        } else {
            croppedBitmapRect.bottom.roundToInt()
        }

        bitmap?.let {

            val matrix = Matrix()
            matrix.postRotate(exifRotation)

            val croppedBitmap = Bitmap.createBitmap(
                it, cropLeft, cropTop, cropRight - cropLeft, cropBottom - cropTop,matrix,true
            )

            /**
            val croppedBitmap = Bitmap.createBitmap(
                it, cropLeft, cropTop, cropRight - cropLeft, cropBottom - cropTop
            )**/
            return CroppedBitmapData(croppedBitmap = croppedBitmap)
        }

        throw IllegalStateException("Bitmap is null.")
    }

Regards!!!

byron-xie commented 3 years ago

解决了没有

Qdafengzi commented 8 months ago

@dan085 Hello friend, how did you finally solve it