您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么用Android Camera實現(xiàn)預(yù)覽框顯示的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇怎么用Android Camera實現(xiàn)預(yù)覽框顯示文章都會有所收獲,下面我們一起來看看吧。
Android要預(yù)覽Camer界面其實非常簡單,只需要幾句話就行。
1、首先要再AndroidManifest.xml中添加權(quán)限
<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進(jìn)行參數(shù)設(shè)置,最后執(zhí)行camera.startPreview
(3)關(guān)閉預(yù)覽框的時候釋放一下對象就行
比如下面的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ù)覽按鈕點擊監(jiān)聽 findViewById(R.id.btnStart).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "btnStart!"); startPreview(); } }); //停止預(yù)覽按鈕點擊監(jiān)聽 findViewById(R.id.btnStop).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i(TAG, "btnStop!"); stopPreview(); } }); //預(yù)覽框狀態(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); //當(dāng)SurefaceTexture可用的時候,可以設(shè)置相機(jī)參數(shù)并打開相機(jī) handleRequestCamera(surface); //handleRequestCamera(mTextureView.getSurfaceTexture()); //如果和mTextureView是同一個類內(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ù)覽的時候,會一直打印 //Log.i(TAG, "onSurfaceTextureUpdated!"); } }); } Camera mCameram; //可以用來對打開的攝像頭進(jìn)行關(guān)閉,釋放 int mCameraId = 0; private void handleRequestCamera(SurfaceTexture texture) { Log.i(TAG, "handleRequestCamera"); //簡單的判斷權(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, "無攝像頭權(quán)限", Toast.LENGTH_LONG).show(); return; } try { //0/1/2 mCameram = Camera.open(mCameraId);//手機(jī)上可以用來切換前后攝像頭,不同的設(shè)備要看底層支持情況 Log.i(TAG, "handleRequestCamera mCameraId = " + mCameraId); Camera.Parameters parameters = mCameram.getParameters(); parameters.setPreviewSize(720, 1280); // parameters.setPreviewSize(1280, 720);//不同的設(shè)備屏幕尺寸不同,有的設(shè)備設(shè)置錯誤的尺寸會崩潰 mCameram.setParameters(parameters); mCameram.setPreviewTexture(texture); mCameram.startPreview(); } catch (Exception error) { Log.e(TAG, "handleRequestCamera error = " + error.getMessage()); } } /** * 開始預(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();//界面退出要釋放對象 } }
需要注意的是,調(diào)用Camera.open之前,要確保預(yù)覽框已經(jīng)準(zhǔn)備好了,
即onSurfaceTextureAvailable方法已經(jīng)回調(diào),正常界面顯示的時候,都是沒有問題的,
但是如果在代碼中,View或者Activity創(chuàng)建的時候調(diào)用Camera.open,這時候是無法預(yù)覽界面的,
如果需要代碼多處,調(diào)用Camera.open,正常做法可以設(shè)置一個全局變量,判斷SurfaceTexture是否可用。
關(guān)于“怎么用Android Camera實現(xiàn)預(yù)覽框顯示”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“怎么用Android Camera實現(xiàn)預(yù)覽框顯示”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。