【发布时间】: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
}
}
}
【问题讨论】:
标签: android-studio kotlin camera android-camera