WebsiteBeaver / android-document-scanner

59 stars 47 forks source link

LifecycleOwners must call register before they are STARTED. #4

Closed koai-dev closed 2 years ago

dmarcs commented 2 years ago

You're probably calling startScan in the wrong place

This should work

package com.your.project

import android.graphics.BitmapFactory
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.websitebeaver.documentscanner.DocumentScanner

class MainActivity : AppCompatActivity() {
    private lateinit var croppedImageView: ImageView

    private val documentScanner = DocumentScanner(
        this,
        { croppedImageResults ->
            // display the first cropped image
            croppedImageView.setImageBitmap(
                BitmapFactory.decodeFile(croppedImageResults.first())
            )
        },
        {
            // an error happened
            errorMessage -> Log.v("documentscannerlogs", errorMessage)
        },
        {
            // user canceled document scan
            Log.v("documentscannerlogs", "User canceled document scan")
        }
    )

    val getDocumentScan = this.registerForActivityResult(
        ActivityResultContracts.StartActivityForResult()
    ) { result: ActivityResult -> documentScanner.handleDocumentScanIntentResult(result) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // cropped image
        croppedImageView = findViewById(R.id.cropped_image_view)
    }

    fun onClickStartScanButton(view: View) {
        getDocumentScan.launch(documentScanner.createDocumentScanIntent())
    }
}
charlo2433 commented 1 year ago

this doesnt work onclick doesnt launch the camera for scanning

upskubra commented 1 year ago

Yesss, it works <3