Android實(shí)現(xiàn)極簡(jiǎn)打開攝像頭
很多時(shí)候忘記Android攝像頭如何打開,查看google文檔的話,發(fā)現(xiàn)太復(fù)雜(只是單純的想打開攝像頭而已,不想添加那么多設(shè)置,添加那么功能),很多博客也是對(duì)官方文檔的小修小改,連方法名都一樣,因此,我決定完成Android相機(jī)最簡(jiǎn)單的打開攝像頭(僅僅打開)。很久沒用忘掉的話,打開鏈接復(fù)制粘貼一下就完事了。
AndroidManifest.xml設(shè)置CAMERA權(quán)限后,在代碼中還要設(shè)置權(quán)限檢查,但是因?yàn)槲疫B權(quán)限檢查都懶得加了,裝好后直接在手機(jī)系統(tǒng)里手動(dòng)允許權(quán)限。
Camera1(已廢棄):
xml中使用SurfaceView作為預(yù)覽View
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <SurfaceView ? ? ? ? android:id="@+id/surfaceView" ? ? ? ? android:layout_width="0dp" ? ? ? ? android:layout_height="0dp" ? ? ? ? app:layout_constraintBottom_toBottomOf="parent" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintRight_toRightOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{
? ? private SurfaceHolder holder;
? ? private Camera camera;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? requestWindowFeature(Window.FEATURE_NO_TITLE);
? ? ? ? getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? SurfaceView surfaceView = findViewById(R.id.surfaceView);
? ? ? ? holder = surfaceView.getHolder();
? ? ? ? holder.addCallback(this);
? ? }
? ? @Override
? ? public void surfaceCreated(SurfaceHolder holder) {
? ? ? ? if(camera == null){
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
? ? ? ? ? ? ? ? camera.setPreviewDisplay(holder);
? ? ? ? ? ? ? ? camera.startPreview();
? ? ? ? ? ? ? ? Camera.Parameters parameters = camera.getParameters();
? ? ? ? ? ? ? ? parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
? ? ? ? ? ? ? ? parameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
? ? ? ? ? ? ? ? camera.setParameters(parameters);
? ? ? ? ? ? ? ? camera.setDisplayOrientation(90);
? ? ? ? ? ? }catch (Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}這樣就完成了最簡(jiǎn)單的打開攝像頭并在手機(jī)中出現(xiàn)畫面。(代碼里去掉2個(gè)接口中未實(shí)現(xiàn)的方法)
Camera2
Android 5.0(API 21)以后,谷歌就決定廢棄原有的Camera API改用Camera2 API,因?yàn)楣δ芨鼜?qiáng)大

xml使用TextureView作為預(yù)覽(其實(shí)SurfaceView也行,官方的Demo是用TextureView的一個(gè)子類):
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? tools:context=".MainActivity"> ? ? <TextureView ? ? ? ? android:id="@+id/surfaceView" ? ? ? ? android:layout_width="0dp" ? ? ? ? android:layout_height="0dp" ? ? ? ? app:layout_constraintBottom_toBottomOf="parent" ? ? ? ? app:layout_constraintLeft_toLeftOf="parent" ? ? ? ? app:layout_constraintRight_toRightOf="parent" ? ? ? ? app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
? ? private TextureView textureView;
? ? private CaptureRequest.Builder builder;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? textureView = findViewById(R.id.surfaceView);
? ? ? ? textureView.setSurfaceTextureListener(this);
? ? }
? ? @Override
? ? public void onSurfaceTextureAvailable(final SurfaceTexture surface, int width, int height) {
? ? ? ? CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
? ? ? ? if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA)
? ? ? ? ? ? ? ? != PackageManager.PERMISSION_GRANTED) {
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? try {
? ? ? ? ? ? manager.openCamera("0", new CameraDevice.StateCallback() {
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onOpened(@NonNull CameraDevice camera) {
? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
? ? ? ? ? ? ? ? ? ? surfaceTexture.setDefaultBufferSize(1440,1080);
? ? ? ? ? ? ? ? ? ? Surface surface = new Surface(surfaceTexture);
? ? ? ? ? ? ? ? ? ? builder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
? ? ? ? ? ? ? ? ? ? builder.addTarget(surface);
? ? ? ? ? ? ? ? ? ? camera.createCaptureSession(Arrays.asList(surface),
? ? ? ? ? ? ? ? ? ? ? ? ? ? new CameraCaptureSession.StateCallback() {
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onConfigured(@NonNull CameraCaptureSession session) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? session.setRepeatingRequest(builder.build(), null, null);
? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? public void onConfigureFailed(@NonNull CameraCaptureSession session) {
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }, null);
? ? ? ? ? ? ? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onDisconnected(@NonNull CameraDevice camera) {
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void onError(@NonNull CameraDevice camera, int error) {
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }, null);
? ? ? ? } catch (CameraAccessException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}這樣就成功使用Camera2的API打開并預(yù)覽了(代碼里去掉3個(gè)接口中未實(shí)現(xiàn)的方法)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android CameraX打開攝像頭預(yù)覽教程
- Android?CameraX?打開攝像頭預(yù)覽功能
- Androidstudio調(diào)用攝像頭拍照并保存照片
- Android調(diào)用外置攝像頭的方法
- Android調(diào)用手機(jī)攝像頭拍照和錄音功能
- Android實(shí)現(xiàn)調(diào)用手機(jī)攝像頭錄像限制錄像時(shí)長(zhǎng)
- Android調(diào)用手機(jī)攝像頭的方法
- android開發(fā)之調(diào)用手機(jī)的攝像頭使用MediaRecorder錄像并播放
- Android開發(fā)教程之調(diào)用攝像頭功能的方法詳解
- Android實(shí)現(xiàn)調(diào)用攝像頭進(jìn)行拍照功能
相關(guān)文章
Android組件之BroadcastReceiver廣播接收者
這篇文章主要為大家介紹了Android組件之BroadcastReceiver廣播接收者實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
詳解Android開發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限)
這篇文章主要介紹了詳解Android開發(fā)錄音和播放音頻的步驟(動(dòng)態(tài)獲取權(quán)限),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Android編程實(shí)現(xiàn)滑動(dòng)開關(guān)組件功能【附源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)滑動(dòng)開關(guān)組件功能,結(jié)合實(shí)例形式詳細(xì)分析了Android滑動(dòng)開關(guān)組件的簡(jiǎn)單布局與功能實(shí)現(xiàn)技巧,并附帶完整實(shí)例源碼供讀者下載參考,需要的朋友可以參考下2018-01-01
Android開發(fā)之React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標(biāo)消息提示
這篇文章主要介紹了React Navigation 導(dǎo)航欄樣式調(diào)整+底部角標(biāo)消息提示的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-05-05
Android編程獲取網(wǎng)絡(luò)時(shí)間實(shí)例分析
這篇文章主要介紹了Android編程獲取網(wǎng)絡(luò)時(shí)間,結(jié)合實(shí)例形式對(duì)比分析了Android通過訪問網(wǎng)絡(luò)及通過GPS獲取網(wǎng)絡(luò)時(shí)間的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-01-01
Android實(shí)現(xiàn)webview實(shí)例代碼
本篇文章主要介紹了Android實(shí)現(xiàn)webview實(shí)例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2017-06-06
在android中實(shí)現(xiàn)類似uc和墨跡天氣的左右拖動(dòng)效果
本文主要介紹下怎樣在android實(shí)現(xiàn)uc和墨跡天氣那樣的左右拖動(dòng)效果,具體代碼如下,感興趣的朋友可以參考下哈2013-06-06
android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了android viewpager實(shí)現(xiàn)豎直滑動(dòng)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android scrollview如何監(jiān)聽滑動(dòng)狀態(tài)
這篇文章主要介紹了Android scrollview監(jiān)聽滑動(dòng)狀態(tài)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

