问题
I want to take a photo and crop out a square of 25x25 dp from the center using CameraX. I have read that cropping is possible using ImageCapture but unfortunately there are almost no similar examples out there so far.
val imageCaptureConfig = ImageCaptureConfig.Builder().apply {
setTargetAspectRatio(Rational(1, 1))
setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
}.build()
val imageCapture = ImageCapture(imageCaptureConfig)
btn_take_photo.setOnClickListener {
imageCapture.takePicture(
object : ImageCapture.OnImageCapturedListener() {
override fun onCaptureSuccess(image: ImageProxy?, rotationDegrees: Int) {
super.onCaptureSuccess(image, rotationDegrees)
// image manipulation here?
}
}
)
}
回答1:
You can convert the image to a Bitmap and then crop it.
Bitmap cropImage(Image image, int rotationDegree, int xOffset, int yOffset, int cropWidth, int cropHeight) {
// 1 - Convert image to Bitmap
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
// 2 - Rotate the Bitmap
if(rotationDegree != 0) {
Matrix rotationMatrix = new Matrix();
rotationMatrix.postRotate(rotationDegree);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), rotationMatrix, true);
}
// 3 - Crop the Bitmap
bitmap = Bitmap.createBitmap(bitmap, xOffset, yOffset, cropWidth, cropHeight);
return bitmap;
}
回答2:
You can use this function to cropping the Image after capture the Image:
private fun cropImage(bitmap: Bitmap, frame: View, reference: View): ByteArray {
val heightOriginal = frame.height
val widthOriginal = frame.width
val heightFrame = reference.height
val widthFrame = reference.width
val leftFrame = reference.left
val topFrame = reference.top
val heightReal = bitmap.height
val widthReal = bitmap.width
val widthFinal = widthFrame * widthReal / widthOriginal
val heightFinal = heightFrame * heightReal / heightOriginal
val leftFinal = leftFrame * widthReal / widthOriginal
val topFinal = topFrame * heightReal / heightOriginal
val bitmapFinal = Bitmap.createBitmap(
bitmap,
leftFinal, topFinal, widthFinal, heightFinal
)
val stream = ByteArrayOutputStream()
bitmapFinal.compress(
Bitmap.CompressFormat.JPEG,
100,
stream
) //100 is the best quality possibe
return stream.toByteArray()
}
Crop an image taking a reference a view parent like a frame and a view child like final reference
- param
bitmap
image to crop - param
frame
where the image is set it - param
reference
frame to take reference for a crop the image return
image already cropped
回答3:
You can get the planes from the image and crop them manually:
private fun cropByteArray(array : ByteArray, cropRect: Rect): ByteArray {
val croppedArray = ByteArray(cropRect.width()*cropRect.height())
val imageWidth = 640
var i = 0
array.forEachIndexed { index, byte ->
val x = index % imageWidth
val y = index / imageWidth
if (cropRect.left <= x && x < cropRect.right && cropRect.top <= y && y < cropRect.bottom) {
croppedArray[i] = byte
i++
}
}
return croppedArray
}
......
val buffer = image.planes[0].buffer
val imageByteArray = buffer.toByteArray()
val data = cropByteArray(imageByteArray, cropRect)
来源:https://stackoverflow.com/questions/57222800/android-how-to-crop-images-using-camerax