Android?Camera1實現(xiàn)預覽框顯示
本文實例為大家分享了Android Camera1實現(xiàn)預覽框顯示的具體代碼,供大家參考,具體內容如下
Android要預覽Camer界面其實非常簡單,只需要幾句話就行。
1、首先要再AndroidManifest.xml中添加權限
<uses-permission android:name="android.permission.CAMERA"/>
2、創(chuàng)建一個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對象
(2)Camera進行參數(shù)設置,最后執(zhí)行camera.startPreview
(3)關閉預覽框的時候釋放一下對象就行
比如下面的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; //預覽框對象
? ? @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() {
? ? ? ? //預覽按鈕點擊監(jiān)聽
? ? ? ? findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? Log.i(TAG, "btnStart!");
? ? ? ? ? ? ? ? startPreview();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //停止預覽按鈕點擊監(jiān)聽
? ? ? ? findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? Log.i(TAG, "btnStop!");
? ? ? ? ? ? ? ? stopPreview();
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //預覽框狀態(tài)監(jiān)聽
? ? ? ? mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
? ? ? ? ? ? ? ? Log.i(TAG, "onSurfaceTextureAvailable width = " + width + ",height = " + height);
? ? ? ? ? ? ? ? //當SurefaceTexture可用的時候,可以設置相機參數(shù)并打開相機
? ? ? ? ? ? ? ? handleRequestCamera(surface);
? ? ? ? ? ? ? ? //handleRequestCamera(mTextureView.getSurfaceTexture()); //如果和mTextureView是同一個類內,效果和上面是一樣的
? ? ? ? ? ? }
? ? ? ? ? ? @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) {
? ? ? ? ? ? ? ? //正常預覽的時候,會一直打印
? ? ? ? ? ? ? ? //Log.i(TAG, "onSurfaceTextureUpdated!");
? ? ? ? ? ? }
? ? ? ? });
? ? }
? ? Camera mCameram; //可以用來對打開的攝像頭進行關閉,釋放
? ? int mCameraId = 0;
? ? private void handleRequestCamera(SurfaceTexture texture) {
? ? ? ? Log.i(TAG, "handleRequestCamera");
? ? ? ? //簡單的判斷權限
? ? ? ? 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, "無攝像頭權限", Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? //0/1/2
? ? ? ? ? ? mCameram = Camera.open(mCameraId);//手機上可以用來切換前后攝像頭,不同的設備要看底層支持情況
? ? ? ? ? ? Log.i(TAG, "handleRequestCamera mCameraId = " + mCameraId);
? ? ? ? ? ? Camera.Parameters parameters = mCameram.getParameters();
? ? ? ? ? ? parameters.setPreviewSize(720, 1280);
// ? ? ? ? ? ?parameters.setPreviewSize(1280, 720);//不同的設備屏幕尺寸不同,有的設備設置錯誤的尺寸會崩潰
? ? ? ? ? ? mCameram.setParameters(parameters);
? ? ? ? ? ? mCameram.setPreviewTexture(texture);
? ? ? ? ? ? mCameram.startPreview();
? ? ? ? } catch (Exception error) {
? ? ? ? ? ? Log.e(TAG, "handleRequestCamera error = " + error.getMessage());
? ? ? ? }
? ? }
? ? /**
? ? ?* 開始預覽
? ? ?*/
? ? private void startPreview() {
? ? ? ? Log.i(TAG, "startPreview");
? ? ? ? SurfaceTexture mSurfaceTexture = mTextureView.getSurfaceTexture();
? ? ? ? handleRequestCamera(mSurfaceTexture);
? ? }
? ? /**
? ? ?* 停止預覽
? ? ?* 根據(jù)情況選擇是否釋放,
? ? ?* 可以stopPreview,停止預覽界面,后面用startPreview可以恢復預覽界面
? ? ?*/
? ? private void stopPreview() {
? ? ? ? if (mCameram != null) {
? ? ? ? ? ? mCameram.stopPreview();
? ? ? ? ? ? mCameram.release();
? ? ? ? ? ? mCameram = null;
? ? ? ? }
? ? }
? ? @Override
? ? protected void onDestroy() {
? ? ? ? super.onDestroy();
? ? ? ? stopPreview();//界面退出要釋放對象
? ? }
}需要注意的是,調用Camera.open之前,要確保預覽框已經(jīng)準備好了,
即onSurfaceTextureAvailable方法已經(jīng)回調,正常界面顯示的時候,都是沒有問題的,
但是如果在代碼中,View或者Activity創(chuàng)建的時候調用Camera.open,這時候是無法預覽界面的,
如果需要代碼多處,調用Camera.open,正常做法可以設置一個全局變量,判斷SurfaceTexture是否可用。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載
本篇文章主要介紹了android使用PullToRefresh實現(xiàn)下拉刷新和上拉加載,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
DrawerLayout的簡單使用及側滑菜單實現(xiàn)詳解
這篇文章主要為大家介紹了DrawerLayout的簡單使用及側滑菜單實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04
Android 詳解自定義圓角輸入框和按鈕的實現(xiàn)流程
對于安卓程序員來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實現(xiàn),而用自定義view,簡直分分鐘,今天我們來實現(xiàn)自定義圓角輸入框和按鈕,大家可以跟著練習,掌握技巧2021-11-11
Android開發(fā)實現(xiàn)ListView點擊展開收起效果示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)ListView點擊展開收起效果,結合實例形式分析了Android ListView控件的布局及事件響應相關操作技巧,需要的朋友可以參考下2019-03-03

