我正在使用 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)
输出日志:输出错误:
为什么 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/