Android?Camera1實(shí)現(xiàn)預(yù)覽框顯示
本文實(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)文章
android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載
本篇文章主要介紹了android使用PullToRefresh實(shí)現(xiàn)下拉刷新和上拉加載,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android 應(yīng)用更換皮膚實(shí)現(xiàn)方法
本文主要介紹Android 應(yīng)用更換皮膚,Android應(yīng)用如果想更換皮膚這里幫大家整理了相關(guān)資料,有需要的小伙伴可以參考下2016-08-08DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了DrawerLayout的簡(jiǎn)單使用及側(cè)滑菜單實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android 詳解自定義圓角輸入框和按鈕的實(shí)現(xiàn)流程
對(duì)于安卓程序員來(lái)說(shuō),自定義view簡(jiǎn)直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實(shí)現(xiàn),而用自定義view,簡(jiǎn)直分分鐘,今天我們來(lái)實(shí)現(xiàn)自定義圓角輸入框和按鈕,大家可以跟著練習(xí),掌握技巧2021-11-11Android開(kāi)發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開(kāi)收起效果示例
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開(kāi)收起效果,結(jié)合實(shí)例形式分析了Android ListView控件的布局及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03