欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android?Camera1實(shí)現(xiàn)預(yù)覽框顯示

 更新時(shí)間:2022年05月19日 11:33:18   作者:崢嶸life  
這篇文章主要為大家詳細(xì)介紹了Android?Camera1實(shí)現(xiàn)預(yù)覽框顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android Camera1實(shí)現(xiàn)預(yù)覽框顯示的具體代碼,供大家參考,具體內(nèi)容如下

Android要預(yù)覽Camer界面其實(shí)非常簡(jiǎn)單,只需要幾句話(huà)就行。

1、首先要再AndroidManifest.xml中添加權(quán)限

<uses-permission android:name="android.permission.CAMERA"/>

2、創(chuàng)建一個(gè)xml包含控件TextureView

比如activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent">
? ? <TextureView
? ? ? ? android:id="@+id/textureView"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="match_parent" />
? ? <Button
? ? ? ? android:id="@+id/btnStop"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_margin="0.8dp"
? ? ? ? android:text="stop preview"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_alignParentEnd="true"/>
? ? <Button
? ? ? ? android:id="@+id/btnStart"
? ? ? ? android:layout_width="wrap_content"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:layout_margin="0.8dp"
? ? ? ? android:text="start preview"
? ? ? ? android:layout_alignParentBottom="true"
? ? ? ? android:layout_toStartOf="@id/btnStop"/>

</RelativeLayout>

3、在Activity創(chuàng)建使用Camera

(1)使用Camera.open(0)獲取Camera對(duì)象
(2)Camera進(jìn)行參數(shù)設(shè)置,最后執(zhí)行camera.startPreview
(3)關(guān)閉預(yù)覽框的時(shí)候釋放一下對(duì)象就行

比如下面的MainActivity.java代碼:

package com.lwz.camera;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.TextureView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

? ? private static final String TAG = "Camera2Test";
? ? private TextureView mTextureView; //預(yù)覽框?qū)ο?

? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? Log.e(TAG, "onCreate!");
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? intiView();
? ? ? ? initEvent();
? ? }

? ? private void intiView() {
? ? ? ? mTextureView = (TextureView) findViewById(R.id.textureView);
? ? }

? ? private void initEvent() {
? ? ? ? //預(yù)覽按鈕點(diǎn)擊監(jiān)聽(tīng)
? ? ? ? findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? Log.i(TAG, "btnStart!");
? ? ? ? ? ? ? ? startPreview();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //停止預(yù)覽按鈕點(diǎn)擊監(jiān)聽(tīng)
? ? ? ? findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? Log.i(TAG, "btnStop!");
? ? ? ? ? ? ? ? stopPreview();
? ? ? ? ? ? }
? ? ? ? });

? ? ? ? //預(yù)覽框狀態(tài)監(jiān)聽(tīng)
? ? ? ? mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
? ? ? ? ? ? ? ? Log.i(TAG, "onSurfaceTextureAvailable width = " + width + ",height = " + height);
? ? ? ? ? ? ? ? //當(dāng)SurefaceTexture可用的時(shí)候,可以設(shè)置相機(jī)參數(shù)并打開(kāi)相機(jī)
? ? ? ? ? ? ? ? handleRequestCamera(surface);
? ? ? ? ? ? ? ? //handleRequestCamera(mTextureView.getSurfaceTexture()); //如果和mTextureView是同一個(gè)類(lèi)內(nèi),效果和上面是一樣的
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
? ? ? ? ? ? ? ? Log.i(TAG, "onSurfaceTextureSizeChanged width = " + width + ",height = " + height);
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
? ? ? ? ? ? ? ? Log.i(TAG, "onSurfaceTextureDestroyed!");
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }

? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
? ? ? ? ? ? ? ? //正常預(yù)覽的時(shí)候,會(huì)一直打印
? ? ? ? ? ? ? ? //Log.i(TAG, "onSurfaceTextureUpdated!");
? ? ? ? ? ? }
? ? ? ? });
? ? }


? ? Camera mCameram; //可以用來(lái)對(duì)打開(kāi)的攝像頭進(jìn)行關(guān)閉,釋放
? ? int mCameraId = 0;

? ? private void handleRequestCamera(SurfaceTexture texture) {
? ? ? ? Log.i(TAG, "handleRequestCamera");
? ? ? ? //簡(jiǎn)單的判斷權(quán)限
? ? ? ? if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? ActivityCompat.requestPermissions(this, new String[]{"android.permission.CAMERA"}, 100);
? ? ? ? ? ? Log.e(TAG, "openCamera no Permission!");
? ? ? ? ? ? Toast.makeText(this, "無(wú)攝像頭權(quán)限", Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? //0/1/2
? ? ? ? ? ? mCameram = Camera.open(mCameraId);//手機(jī)上可以用來(lái)切換前后攝像頭,不同的設(shè)備要看底層支持情況
? ? ? ? ? ? Log.i(TAG, "handleRequestCamera mCameraId = " + mCameraId);
? ? ? ? ? ? Camera.Parameters parameters = mCameram.getParameters();
? ? ? ? ? ? parameters.setPreviewSize(720, 1280);
// ? ? ? ? ? ?parameters.setPreviewSize(1280, 720);//不同的設(shè)備屏幕尺寸不同,有的設(shè)備設(shè)置錯(cuò)誤的尺寸會(huì)崩潰
? ? ? ? ? ? mCameram.setParameters(parameters);
? ? ? ? ? ? mCameram.setPreviewTexture(texture);
? ? ? ? ? ? mCameram.startPreview();
? ? ? ? } catch (Exception error) {
? ? ? ? ? ? Log.e(TAG, "handleRequestCamera error = " + error.getMessage());
? ? ? ? }
? ? }

? ? /**
? ? ?* 開(kāi)始預(yù)覽
? ? ?*/
? ? private void startPreview() {
? ? ? ? Log.i(TAG, "startPreview");
? ? ? ? SurfaceTexture mSurfaceTexture = mTextureView.getSurfaceTexture();
? ? ? ? handleRequestCamera(mSurfaceTexture);
? ? }

? ? /**
? ? ?* 停止預(yù)覽
? ? ?* 根據(jù)情況選擇是否釋放,
? ? ?* 可以stopPreview,停止預(yù)覽界面,后面用startPreview可以恢復(fù)預(yù)覽界面
? ? ?*/
? ? private void stopPreview() {
? ? ? ? if (mCameram != null) {
? ? ? ? ? ? mCameram.stopPreview();
? ? ? ? ? ? mCameram.release();
? ? ? ? ? ? mCameram = null;
? ? ? ? }
? ? }

? ? @Override
? ? protected void onDestroy() {
? ? ? ? super.onDestroy();
? ? ? ? stopPreview();//界面退出要釋放對(duì)象
? ? }
}

需要注意的是,調(diào)用Camera.open之前,要確保預(yù)覽框已經(jīng)準(zhǔn)備好了,
即onSurfaceTextureAvailable方法已經(jīng)回調(diào),正常界面顯示的時(shí)候,都是沒(méi)有問(wèn)題的,
但是如果在代碼中,View或者Activity創(chuàng)建的時(shí)候調(diào)用Camera.open,這時(shí)候是無(wú)法預(yù)覽界面的,
如果需要代碼多處,調(diào)用Camera.open,正常做法可以設(shè)置一個(gè)全局變量,判斷SurfaceTexture是否可用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論