问题描述
我正在开发一个项目,该项目使用 mlkit 检测图像中的身份证。输出是只有身份证的裁剪图像。 当我从给定的图像文件的 uri 创建输入位图时,输入位图的方向错误。
var inputBitmapWithWrongOrientation1: Bitmap = MediaStore.Images.Media.getBitmap(requireContext().contentResolver,inputUri)
// var inputBitmapWithWrongOrientation2: Bitmap = BitmapFactory.decodeFile(photoFile.path)
据我所知,输入文件以正确的方向(即 0 度)保存。所以无论你做什么 (ExifInterFace
,MediaStore contentresolver query
) 他们都会返回 0 度。 MlKit 读取输入 uri 并以正确的方向处理所有内容。所以detectedobject.boundaryBox 是正确的。但是 inputBitmapWithWrongOrientation1 的方向错误(90 或 270 度)。因此,尝试使用该边界框裁剪 inputBitmapWithWrongOrientation1 裁剪图像的错误部分。
因此,inputBitmapWithWrongOrientation1
始终处于错误的方向(90 度或 270 度方向)。
如何获得方向正确的 inputBitmap? 相关代码段:
val inputBitmap: Bitmap = getBitmapWithCorrectOrientation(inputUri) // <-- what should be this?
val inputimage: Inputimage = Inputimage.fromFilePath(contentResolver,inputUri) // for mlkit
objectDetector.process(inputimage).addOnSuccessListener { detectedobjectsList ->
Log.e(TAG,"detectedobjectsList.size = ${detectedobjectsList.size}")
for(detectedobject in detectedobjectsList) {
val bBox = detectedobject.boundingBox
val outputBitmap = Bitmap.createBitmap(inputBitmap,bBox.left,bBox.top,bBox.width(),bBox.height())
outputIv.setimageBitmap(outputBitmap)
}
}
编辑
我从 CameraX 获取图像 uri:
val photoFile = File(requireContext().getExternalFilesDir(""),SimpleDateFormat(FILENAME_FORMAT,Locale.US
).format(System.currentTimeMillis()) + ".jpg") // <-- this is the captured image / input image save location
// create output options object which contains file + Metadata
val outputoptions = ImageCapture.OutputFileOptions.Builder(photoFile).build()
imageCapture.takePicture(
outputoptions,ContextCompat.getMainExecutor(requireContext()),object : ImageCapture.OnImageSavedCallback{
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
var inputUri = outputFileResults.savedUri
// then the rest of the code below
}
}
// ... ...
)
如您所见,图片是保存在 .jpg
中的 photoFile
文件。 CameraX 将照片保存在此。我已经检查过照片是否正确拍摄。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)