java - IllegalArgumentException : width and height must be > 0 - error when loading an image using Imgcodecs. 已读

标签 java android opencv kotlin mobile

我正在使用 Imgcodecs.imread从手机的图库中上传图像以对该图像执行分割。但是,在创建位图以将其显示在屏幕上时,我收到了 error: IllegalArgumentException: width and height must be> 0.我很快意识到Imgcodecs.imread没有正确打开图像,因为我使用日志查看变量的输出。如下。

// Load the image
   val srcOriginal = Imgcodecs.imread(currentPhotoPath)
    val src: Mat = srcOriginal.clone()
    Log.i("teste", src.toString())
    Log.i("teste", srcOriginal.toString())
    Log.i("teste", src.rows().toString()+"\n"+ src.cols())
    // Create a blank image of zeros (same dimension as img)
    // It should be grayscale (1 color channel)
    val markers = Mat.zeros(srcOriginal.rows(), srcOriginal.cols(), CvType.CV_32F)

    // This step is manual. The goal is to find the points
    // which create the result we want. I suggest using a
    // tool to get the pixel coordinates.

    // Dictate the area of interest
    for(x in my_canvas.pointsToDrawX.indices) {
            markers.put(
                my_canvas.pointsToDrawX.get(x).toInt(),
                my_canvas.pointsToDrawY.get(x).toInt(),
                255.0
            )
    }

    val src1 = srcOriginal.clone()
    val bmpOut = Bitmap.createBitmap(src1.cols(), src1.rows(), Bitmap.Config.RGB_565)
    //Create Bitmap
    Utils.matToBitmap(src1, bmpOut)
    image.setImageBitmap(bmpOut)
输出日志:
enter image description here
输出错误:
enter image description here
为什么 Imgcodecs.imread 不能正确打开图像?

最佳答案

您需要添加文件读取和写入权限。为此,您可以使用以下库: implementation 'com.tbruyelle.rxpermissions2: rxpermissions: 0.9.4@aar'和实现"io.reactivex.rxjava2: rxandroid: 2.0.1" .这样,在打开图像时,您将使用以下代码检查您是否具有读写权限:

private fun tryOpenFile() {

    rxPermissions
        .request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        .subscribe({ granted ->
            if (granted) {
                val getPictureIntent = Intent(Intent.ACTION_GET_CONTENT).apply {
                    type = "image/*"}
                val pickPictureIntent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
                val chooserIntent = Intent.createChooser(getPictureIntent, "Select Image")
                    .apply { putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(pickPictureIntent)) }
                startActivityForResult(chooserIntent, PICK_IMAGE_INTENT)
            } else {
                Toast.makeText(this@MainActivity, "App needs permission to read/write external storage", Toast.LENGTH_SHORT).show()
            }
        })
        .addTo(disposables)

}

我希望它有效。

关于java - IllegalArgumentException : width and height must be > 0 - error when loading an image using Imgcodecs. 已读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62072345/

相关文章:

java - 从 json 对象中提取值以达到 Intent

java - 原子文件写入操作(跨平台)

Android(Fragment) - 是否建议在 onActivityCreated 方法中初始化 View 对象?

java - 将 transient 属性映射到别名 - Spring JPA

Java Swing (FileReader) 读取 text.file 并写入 JList - 随机工作 Oo

java - 使用 Oauth2.0/OpenId 混合验证 Android 应用程序和 Web 应用程序(后端服务器)

android - 滚动 ListView 时数据解释错误

image-processing - 在opencv中查找轮廓的长度

python-3.x - cv2.error : OpenCV(4. 0.0) (-215 :Assertion failed) ! _src.empty() 在函数 'cv::cvtColor' 中

c++ - warpPerspective 和 perspectiveTransform 有什么区别?