如何在android studio中修复CameraSource预览方向

Posted

技术标签:

【中文标题】如何在android studio中修复CameraSource预览方向【英文标题】:how to fix CameraSource preview orientation in android studio 【发布时间】:2021-09-04 09:46:44 【问题描述】:

所以我在我的应用程序中使用CameraSource 来预览相机扫描,但问题是相机预览总是水平的。为了更清楚地看这张照片: Preview

我的意思是预览总是倾斜的。如何使相机扫描预览向上(正常)? 这是我当前的代码:

private fun setupCameraSource() 
        cameraSource = CameraSource.Builder(activity, cameraSourceCustomDetector)
            .setAutoFocusEnabled(true).setRequestedFps(10F)
            .setFacing(CameraSource.CAMERA_FACING_BACK).build()
    

或者如果整个代码有帮助,我不知道我是否做错了。

class colorDetector(activity: Activity, cameraPreview: ImageView?, editCameraPreview: ImageView?) 

    private val TAG = "colorDetector"
    private var  bitmap:Bitmap? = null
    private val FPS: Number = 20

    private var cameraSource: CameraSource? = null
    private var cameraSourceCustomDetector: CustomDetector? = null
    private var editCameraPreview: ImageView? = null
    //private var visionUtilities: VisionUtilities? = null
    private var activity: Activity? = null
    private var cameraPreview: ImageView? = null

    init 
        this.activity = activity
        this.cameraPreview = cameraPreview
        this.editCameraPreview = editCameraPreview
        cameraSourceCustomDetector = CustomDetector()
        setupCameraSource()
    

    private fun setupCameraSource() 
        cameraSource = CameraSource.Builder(activity, cameraSourceCustomDetector)
            .setAutoFocusEnabled(true).setRequestedFps(10F)
            .setFacing(CameraSource.CAMERA_FACING_BACK).build()
    

    fun start(context: Context) 
        try 
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) 
                cameraSource?.start()
            
         catch (e: IOException) 
            Log.d(TAG, "Couldn't start camera")
        
    

    fun stop() 
        cameraSource!!.stop()
    

    fun saveImageToStorage() 
        saveImgInStorage(bitmap!!,activity!!)
    
    fun uploadImageToFirebase(type: uploadType) 
        uploadPictureToFirebaseStorage(activity!!,bitmap,null,type)
    
    inner class CustomDetector : Detector<Point>() 

        @RequiresApi(Build.VERSION_CODES.O)
        override fun detect(frame: Frame?): SparseArray<Point>? 

            val byteBuffer: ByteBuffer = frame!!.grayscaleImageData
            val bytes: ByteArray = byteBuffer.array()
            val w = frame.metadata.width
            val h = frame.metadata.height
            val yuvimage = YuvImage(bytes, ImageFormat.NV21, w, h, null)
            val baos = ByteArrayOutputStream()
            yuvimage.compressToJpeg(
                Rect(0, 0, w, h),
                100,
                baos
            ) // Where 100 is the quality of the generated jpeg

            val jpegArray = baos.toByteArray()
             bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.size)
            activity?.runOnUiThread(Runnable getEditedImg(bitmap!!,w,h, cameraPreview!!, editCameraPreview!!,
                activity!!
            ) )

            return null

        


    


【问题讨论】:

【参考方案1】:

好吧,最后经过很长时间的搜索,我决定这样做,我不确定这是否是理想的方式,但如果有人想要,这是我所做的解决方案:

我创建了一个名为 rotate 的函数:

// rotates a 'bitmap' clockwise direction by 'degree'
fun rotate(bitmap: Bitmap, degree: Float): Bitmap? 
        val matrix = Matrix()
        matrix.postRotate(degree)
        val resizedBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.width, bitmap.width, false)
        return Bitmap.createBitmap(resizedBitmap, 0, 0, resizedBitmap.width, resizedBitmap.height, matrix, true)

并用rotate(bitmap, 90F)替换了bitmap的3个地方。

【讨论】:

以上是关于如何在android studio中修复CameraSource预览方向的主要内容,如果未能解决你的问题,请参考以下文章

Gradle 在 Android Studio 中搞砸了我的应用程序,如何修复?

如何修复部分安装的 SDK android studio

ANDROID STUDIO:如何修复我的 ScrollView?

如何在运行应用程序时修复 android studio 版本 3.6.2 中的“无法解析结果路径字符串:”错误?

如何修复 Windows 10 上的 Android Studio adb 错误?

如何使用 Android Studio 3.3 修复即时应用程序