簡體   English   中英

CameraX PreviewView 到位圖而不是文件

[英]CameraX PreviewView to Bitmap instead of a File

我知道還有其他問題已經回答了很多次,我試圖找到答案,但我最不明白。 我想讓相機拍照,但不是制作物理圖像文件,而是希望它直接將其制作成位圖並從不同的活動中將其顯示在 ImageView 中,但是我不斷地Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference ,那是因為viewFinder.bitmap是一個 null 並決定創建一個單獨的函數getBitmap(): Bitmap? . 我不知道它希望我誠實地做什么或由此產生任何線索,我想要做的就是將照片作為位圖直接發送到 ImageView。 先感謝您。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.camerax5, PID: 5472
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.camerax5/com.example.camerax5.VirtualPreview}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3141)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3284)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1972)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7179)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:975)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
        at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:164)
        at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:157)
        at android.content.Context.obtainStyledAttributes(Context.java:677)
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
        at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:633)
        at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
        at com.example.camerax5.MainActivity.getBitmap(MainActivity.kt:118)
        at com.example.camerax5.VirtualPreview.onCreate(VirtualPreview.kt:19)
        at android.app.Activity.performCreate(Activity.java:7335)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1275)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3121)

所以問題出現在這一行:

viewFinder = findViewById(R.id.previewView)

這是我試圖做的完整代碼:

MainActivity.kt 和 MainActivity 代碼下面是另一個具有另一個活動的類,用於在 ImageView 中顯示位圖,我添加這種方式是因為我不想將它們分開並使它們變得混亂:

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

        viewFinder = findViewById(R.id.previewView)

        //Permissions
        if (allPermissions()) {
            startCamera()
        } else {
            ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
        }

        //Capture button
        val captureBtn: Button = findViewById(R.id.captureButton)
        captureBtn.setOnClickListener {
            takePhoto()
        }

        outputDirectory = outputDirectoryFolder()

        cameraExecutor = Executors.newSingleThreadExecutor()
    }

private fun takePhoto() {
        Toast.makeText(baseContext, "Processing ..", Toast.LENGTH_SHORT).show()

        //Directly to open the new Activity
        val intent = Intent(this, VirtualPreview::class.java)
        startActivity(intent)
    }

    fun getBitmap(): Bitmap? {
        //Preview to Bitmap
        viewFinder = findViewById(R.id.previewView)
        val source = viewFinder.bitmap
        val newWidth = 200
        val newHeight = 200

        val bitmapWidth = source?.width
        val bitmapHeight = source?.height

        //Matrix
        val matrix = Matrix()
        val scaleWidth = newWidth.toFloat() / bitmapWidth!!
        val scaleHeight = newHeight.toFloat() / bitmapHeight!!
        matrix.postScale(scaleWidth, scaleHeight)

        return Bitmap.createBitmap(source, 0, 0, newWidth, newHeight, matrix, true)
    }
}

class VirtualPreview : AppCompatActivity() {
    private lateinit var acceptButton: Button
    private lateinit var denyButton: Button
    private lateinit var imageView: ImageView

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

        val bitmapPreview = MainActivity().getBitmap()

        val drawableBitmap = BitmapDrawable(resources, bitmapPreview)
        imageView = findViewById(R.id.imagePreview)
        imageView.setImageDrawable(drawableBitmap)

        acceptButton = findViewById(R.id.acceptButton)
        acceptButton.setOnClickListener {

        }

        denyButton = findViewById(R.id.denyButton)
        denyButton.setOnClickListener {
            //delete the matrix and returns to the main activity
            finish()
        }
    }
}

val bitmapPreview = MainActivity().getBitmap() - 這樣你就創建了一個MainActivity的新實例,新實例在預覽視圖中沒有任何東西,也不推薦這種方法。 更好地將位圖存儲在 MainActivity 中,然后將該位圖傳遞給構造函數中的VirtualPreview活動(在捆綁中它不起作用)。 對於您的情況,這不會成為問題,因為您將位圖縮放到 200x200。 對於大位圖,存儲在文件中,並使用該文件。 對於您的情況,您可以在 mainactivity 中創建另一個 imageview 並在該 imageview 中顯示位圖,而不是創建另一個活動。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM