Владислав
Владислав

Reputation: 23

Preview in Custom Camera kotlin

Designing custom camera. There was a problem with the Preview. I have 2 classes: CameraActivity, CameraPreview. Same as here.

After capturing photo, the photo is saved, and the preview stops and nothing happens, but after clicking again, there is no photo, and the preview restores the work.

How to fix it? Maybe i don't correctly use Preview Class?

CameraActivity.kt

class CameraActivity(context: Context, frame : FrameLayout) {

    private var mCamera: Camera? = null       
    private lateinit var mPreview: SurfaceView

    init{
        setCamerasId()
        initCamera(mCameraIdBack) 
    }
    private fun getCameraInstance(id : Int): Camera? {
        return try {
            Camera.open(id)
        } catch (e: Exception) {null}
    }

    private fun initCamera(id : Int){
        mCamera = getCameraInstance(id)
        mPreview = CameraPreview(mContext, mCamera!!)
        mFrame.addView(mPreview)
    }

    fun takePhoto() {
        try {
            mCamera?.takePicture(null, null, mPicture)
        } catch (e : Exception){
            Toast.makeText(mContext, "Error capture photo", Toast.LENGTH_SHORT).show()
        }
        try {
            mCamera?.startPreview()
        } catch (e : Exception){
            Toast.makeText(mContext, "Error start preview", Toast.LENGTH_SHORT).show()
        }

        mPictureFile = null
    }
    private val mPicture = Camera.PictureCallback { data, _ ->
        mPictureFile = getOutputMediaFile() ?: run {
            return@PictureCallback
        }
        try {
            Toast.makeText(mContext, "Success", Toast.LENGTH_SHORT).show()
            val fos = FileOutputStream(mPictureFile)
            fos.write(data)
            fos.close()
            addImageToGallery(mPictureFile!!.path, mContext)
            callShutterSound()

        } catch (e: Exception) {}
    }

CameraPreview.kt

class CameraPreview(
        private var mContext: Context,
        private var mCamera: Camera
) : SurfaceView(mContext), SurfaceHolder.Callback {
    private val mHolder: SurfaceHolder = holder.apply {
        addCallback(this@CameraPreview)
        setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS)
    }

    override fun surfaceCreated(holder: SurfaceHolder) {
        mCamera.apply {
            try {
                setParameters()
                setPreviewDisplay(holder)
                startPreview()
            } catch (e: Exception) {}
        }
    }
    override fun surfaceDestroyed(holder: SurfaceHolder) {
        mCamera.release()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
        if (mHolder.surface == null) {
            return
        }
        try {
            mCamera.stopPreview()
        } catch (e: Exception) {}
        mCamera.apply {
            try {
                setParameters()
                setPreviewDisplay(mHolder)
                startPreview()
            } catch (e: Exception) {}
        }
    }
}

Upvotes: 0

Views: 1396

Answers (1)

Владислав
Владислав

Reputation: 23

A delay was needed to correctly display the previews.

Upvotes: 1

Related Questions