【问题标题】:TensorImage cannot load bitmapTensorImage 无法加载位图
【发布时间】:2020-07-18 20:30:08
【问题描述】:

我正在尝试使用 TensorImage.load() 加载用户使用相机应用拍摄的图片位图。当我传入位图时,出现此错误: java.lang.IllegalArgumentException: Only supports loading ARGB_8888 bitmaps

这是我调用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)
        }
    }
}

recognizeImage 函数中,我调用了一个名为inputImageBuffer 的变量,它的类型为TensorImage。我调用load 函数并传递bitmap。这是应用程序崩溃的地方。谁能告诉我如何解决这个问题?

【问题讨论】:

    标签: android image android-studio tensorflow kotlin


    【解决方案1】:

    我通过以最简单的方式更改位图配置解决了这个问题。

    位图 bmp = imageBitmap.copy(Bitmap.Config.ARGB_8888,true) ;

    这里 ii - 位图是不可变的,因此我使用 Bitmap.Config.ARGB_8888 配置和一个新的具有参考的位图进行了复制, 供进一步参考
    https://developer.android.com/reference/android/graphics/Bitmap.Config

    【讨论】:

      【解决方案2】:

      对于其他人来说,这就是我通过更改位图配置解决问题的方法。

      // 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()
              }
      

      【讨论】:

        猜你喜欢
        • 2013-09-13
        • 1970-01-01
        • 1970-01-01
        • 2012-07-26
        • 1970-01-01
        • 2021-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多