相机套件从框架,Kotlin,Android Studio获取图像

时间:2019-01-27 21:21:20

标签: kotlin camera

我正在使用相机套件作为预览层,我是kotlin的新手,已经按照说明进行操作,但是当我触发按钮时,似乎无法从预览图像获取图像。它可以使用该功能,但是具有临时图像的imageview消失了。

下面是我的代码。下面是logcat中的打印内容,以显示我设置的打印行。

 cameraBtn.setOnClickListener {
        println("Tony Took image 1")
            cameraKitView.captureImage(CameraKitView.ImageCallback { cameraKitView, capturedImage ->
                val savedPhoto = File(Environment.getExternalStorageDirectory(), "photo.jpg")
                try {
                    println("Tony got to image 2")
                    val outputStream = FileOutputStream(savedPhoto.getPath())
                    var bitmap = BitmapFactory.decodeFile(savedPhoto.absolutePath)
                    println("Tony got to image 3 in try block")
                    imageTook.setImageBitmap(bitmap)
                    outputStream.write(capturedImage)
                    outputStream.close()
                } catch (e: java.io.IOException) {
                    println("Tony got to image 4 in catch block")
                    e.printStackTrace()
                }
            })
    }
  

2019-01-27 21:11:18.701 8191-8191 / uk.tonymerritt.appchecker I / System.out:Tony Took图像1   2019-01-27 21:11:20.119 8191-8191 / uk.tonymerritt.appchecker I / System.out:托尼获得了图像2   2019-01-27 21:11:20.128 8191-8191 / uk.tonymerritt.appchecker I / System.out:托尼在尝试块中获得了图像3

1 个答案:

答案 0 :(得分:0)

我能够对试图获取文件路径并将其转换为位图的这种方式进行排序,我能够获取CaptureImageImage(即ByteArray)并将其转换为位图。

cameraBtn.setOnClickListener {
        println("Tony Took image 1")

        cameraKitView.captureImage { _, capturedImage ->
            //                    val savedPhoto = File(Environment.getExternalStorageDirectory(), "photo.jpg")
            try {
                println("Tony got to image 2")
                var bmp = BitmapFactory.decodeByteArray(capturedImage, 0, capturedImage.size)

                imageTook.setImageBitmap(
                    Bitmap.createScaledBitmap(
                        bmp,
                        imageTook.width,
                        imageTook.height,
                        false
                    )
                )
                println("Tony got to image 3 in try block")
            } catch (e: java.io.IOException) {
                println("Tony got to image 4 in catch block")
                e.printStackTrace()
            }
        }
    }