網(wǎng)站首頁 編程語言 正文
很多時(shí)候忘記Android攝像頭如何打開,查看google文檔的話,發(fā)現(xiàn)太復(fù)雜(只是單純的想打開攝像頭而已,不想添加那么多設(shè)置,添加那么功能),很多博客也是對官方文檔的小修小改,連方法名都一樣,因此,我決定完成Android相機(jī)最簡單的打開攝像頭(僅僅打開)。很久沒用忘掉的話,打開鏈接復(fù)制粘貼一下就完事了。
AndroidManifest.xml設(shè)置CAMERA權(quán)限后,在代碼中還要設(shè)置權(quán)限檢查,但是因?yàn)槲疫B權(quán)限檢查都懶得加了,裝好后直接在手機(jī)系統(tǒ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(); ? ? ? ? ? ? } ? ? ? ? } ? ? } }
這樣就完成了最簡單的打開攝像頭并在手機(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)的方法)
原文鏈接:https://blog.csdn.net/weixin_45253393/article/details/105323765
相關(guān)推薦
- 2023-07-08 C++在Qt中使用Python報(bào)錯(cuò)
- 2024-01-12 如何理解 Elasticsearch 中的 Indices、Types、Documents、Fiel
- 2023-01-31 C#實(shí)現(xiàn)批量壓縮和解壓縮的示例代碼_C#教程
- 2022-08-21 C語言數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)_C 語言
- 2022-08-15 Python利用Selenium實(shí)現(xiàn)自動化驗(yàn)證登錄
- 2022-07-20 C語言通過三步翻轉(zhuǎn)法實(shí)現(xiàn)單詞倒置詳解_C 語言
- 2022-02-28 Uncaught Error: Module parse failed: Unexpected to
- 2022-05-25 springboot踩坑日記Feign傳遞MultipartFile詳解
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支