Android TensorImage无法加载位图
我正在尝试使用Android TensorImage无法加载位图,android,image,android-studio,tensorflow,kotlin,Android,Image,Android Studio,Tensorflow,Kotlin,我正在尝试使用TensorImage.load()加载用户使用相机应用程序拍摄的图片的位图。当我传入位图时,出现以下错误: java.lang.IllegalArgumentException:仅支持加载ARGB_8888位图 这是我调用load函数时的代码。首先,它以onActivityResult的开头: override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.on
TensorImage.load()
加载用户使用相机应用程序拍摄的图片的位图。当我传入位图时,出现以下错误:
java.lang.IllegalArgumentException:仅支持加载ARGB_8888位图
这是我调用load
函数时的代码。首先,它以onActivityResult的开头:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAMERA_REQUEST_CODE) {
if(Build.VERSION.SDK_INT < 28) {
val foodBitmap = MediaStore.Images.Media.getBitmap(this.contentResolver, Uri.fromFile(photoFile))
// pass this bitmap to the classifier
val predictions = foodClassifier.recognizeImage(foodBitmap, 0)
} else {
val source = ImageDecoder.createSource(this.contentResolver, Uri.fromFile(photoFile))
val foodBitmap = ImageDecoder.decodeBitmap(source)
// pass this bitmap to the classifier
val predictions = foodClassifier.recognizeImage(foodBitmap, 0)
}
}
}
override-on-activityresult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据)
if(请求代码==摄像机请求代码){
如果(Build.VERSION.SDK_INT<28){
val foodBitmap=MediaStore.Images.Media.getBitmap(this.contentResolver,Uri.fromFile(photoFile))
//将此位图传递给分类器
val预测=foodClassifier.RecognitizeImage(foodBitmap,0)
}否则{
val source=ImageDecoder.createSource(this.contentResolver,Uri.fromFile(photoFile))
val foodBitmap=ImageDecoder.decodeBitmap(源)
//将此位图传递给分类器
val预测=foodClassifier.RecognitizeImage(foodBitmap,0)
}
}
}
在recognizeImage
函数中,我调用一个名为inputImageBuffer
的变量,该变量的类型为TensorImage
。我调用load
函数并传递位图
。这就是应用程序崩溃的地方。有人能告诉我如何解决这个问题吗?对于其他人,这就是我通过更改位图配置来解决问题的方法
// Convert the image to a Bitmap
var bitmap: Bitmap? = null
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val source = ImageDecoder.createSource(requireContext().contentResolver, uri!!)
bitmap = ImageDecoder.decodeBitmap(source)
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
} else {
bitmap = MediaStore.Images.Media.getBitmap(requireContext().contentResolver, uri!!)
}
} catch (e: Exception) {
println("Could not convert image to BitMap")
e.printStackTrace()
}
对于其他人来说,这就是我通过更改位图配置来解决问题的方法
// Convert the image to a Bitmap
var bitmap: Bitmap? = null
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
val source = ImageDecoder.createSource(requireContext().contentResolver, uri!!)
bitmap = ImageDecoder.decodeBitmap(source)
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true)
} else {
bitmap = MediaStore.Images.Media.getBitmap(requireContext().contentResolver, uri!!)
}
} catch (e: Exception) {
println("Could not convert image to BitMap")
e.printStackTrace()
}
我通过以最简单的方式更改位图配置来解决这个问题
位图bmp=imageBitmap.copy(Bitmap.Config.ARGB_8888,true)
这里
ii-位图是不可变的,因此我使用Bitmap.Config.ARGB_8888配置创建了一个副本,并使用reference创建了一个新位图,
供进一步参考
我以最简单的方式更改位图配置,解决了这个问题
位图bmp=imageBitmap.copy(Bitmap.Config.ARGB_8888,true)
这里
ii-位图是不可变的,因此我使用Bitmap.Config.ARGB_8888配置创建了一个副本,并使用reference创建了一个新位图,
供进一步参考