Android App开发

您所在的位置:网站首页 ccd相机怎么用前置摄像头拍照的视频 Android App开发

Android App开发

2024-07-16 08:18:01| 来源: 网络整理| 查看: 265

前言

开发环境是Android studio 北极狐,CameraX1.0.0-alpha02,实现语言是Java。

创建工程

1.引入CameraX 在build.gradle添加要引入CameraX的版本。 在这里插入图片描述

//CameraX def camerax_version = "1.0.0-alpha02" implementation "androidx.camera:camera-core:${camerax_version}" implementation "androidx.camera:camera-camera2:${camerax_version}"

2.在AndroidManifest.xml添加打开摄像头的权限和读写文件的权限。 在这里插入图片描述

android:requestLegacyExternalStorage="true"

3.在工程里面新添加一个Activity。 在这里插入图片描述 在这里插入图片描述 4.CameraXDemo代码。

public class IdentificationPhoto extends AppCompatActivity { private int REQUEST_CODE_PERMISSIONS = 101; private final String [] REQUIRED_PERMISSIONS =new String[] {"android.permission.CAMERA","android.permission.WRITE_EXTERNAL_STORAGE"}; TextureView textureView; ImageView cameraFlip; private int backlensfacing = 0; private int flashLamp = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camerax_demo); //去掉导航栏 getSupportActionBar().hide(); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明状态栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明导航栏 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); } textureView = findViewById(R.id.view_camera); cameraFlip = findViewById(R.id.btn_switch_camera); cameraFlip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(backlensfacing == 0) { startCamera(CameraX.LensFacing.FRONT); backlensfacing = 1; } else if(backlensfacing == 1) { startCamera(CameraX.LensFacing.BACK); backlensfacing = 0; } } }); if(allPermissionsGranted()) { startCamera(CameraX.LensFacing.BACK); } else { ActivityCompat.requestPermissions(this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS); } } private void startCamera(CameraX.LensFacing CAMERA_ID) { unbindAll(); Rational aspectRatio = new Rational(textureView.getWidth(), textureView.getHeight()); Size screen = new Size(textureView.getWidth(),textureView.getHeight()); PreviewConfig pConfig; Preview preview; pConfig = new PreviewConfig.Builder().setLensFacing(CAMERA_ID).setTargetAspectRatio(aspectRatio).setTargetResolution(screen).build(); preview = new Preview(pConfig); preview.setOnPreviewOutputUpdateListener(new Preview.OnPreviewOutputUpdateListener() { @Override public void onUpdated(Preview.PreviewOutput output) { ViewGroup parent = (ViewGroup)textureView.getParent(); parent.removeView(textureView); parent.addView(textureView,0); textureView.setSurfaceTexture(output.getSurfaceTexture()); updateTransform(); } }); final ImageCaptureConfig imageCaptureConfig ; imageCaptureConfig= new ImageCaptureConfig.Builder().setCaptureMode(ImageCapture.CaptureMode.MAX_QUALITY).setTargetRotation(getWindowManager().getDefaultDisplay().getRotation()).setLensFacing(CAMERA_ID).build(); final ImageCapture imgCap = new ImageCapture(imageCaptureConfig); findViewById(R.id.btn_flash).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (flashLamp == 0) { flashLamp = 1; imgCap.setFlashMode(FlashMode.OFF); Toast.makeText(getBaseContext(), "Flash Disable", Toast.LENGTH_SHORT).show(); } else if(flashLamp == 1) { flashLamp = 0; imgCap.setFlashMode(FlashMode.ON); Toast.makeText(getBaseContext(), "Flash Enable", Toast.LENGTH_SHORT).show(); } } }); findViewById(R.id.btn_takePict).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { File image = null; String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_"+ timeStamp + "_"; File storageDir = getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); try { image = File.createTempFile( imageFileName, ".jpeg", storageDir); } catch (IOException e) { e.printStackTrace(); } File file = new File(image.getAbsolutePath()); imgCap.takePicture(file, new ImageCapture.OnImageSavedListener() { @Override public void onImageSaved(@NonNull File file) { String msg = "Pic saved at "+ file.getAbsolutePath(); galleryAddPic(file.getAbsolutePath()); Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show(); } @Override public void onError(@NonNull ImageCapture.UseCaseError useCaseError, @NonNull String message, @Nullable Throwable cause) { String msg = "Pic saved at "+ message; Toast.makeText(getBaseContext(), msg,Toast.LENGTH_LONG).show(); if (cause !=null){ cause.printStackTrace(); Toast.makeText(getBaseContext(), cause.toString(),Toast.LENGTH_LONG).show(); } } }); } }); bindToLifecycle(this,preview, imgCap); } private void galleryAddPic(String currentFilePath){ Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); File file = new File (currentFilePath); Uri contentUri = Uri.fromFile(file); mediaScanIntent.setData(contentUri); this.sendBroadcast(mediaScanIntent); //Toast.makeText(getBaseContext(), "saved to gallery",Toast.LENGTH_LONG).show(); } private void updateTransform(){ Matrix mx = new Matrix(); float w = textureView.getMeasuredWidth(); float h = textureView.getMeasuredHeight(); float cX = w / 2f; float cY = h / 2f; int rotationDgr; int rotation = (int)textureView.getRotation(); switch (rotation){ case Surface.ROTATION_0: rotationDgr = 0; break; case Surface.ROTATION_90: rotationDgr = 90; break; case Surface.ROTATION_180: rotationDgr = 180; break; case Surface.ROTATION_270: rotationDgr = 270; break; default: return; } mx.postRotate((float)rotationDgr, cX,cY); textureView.setTransform(mx); } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if(requestCode == REQUEST_CODE_PERMISSIONS){ if (allPermissionsGranted()) { startCamera(CameraX.LensFacing.BACK); } else{ Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show(); finish(); } } } private boolean allPermissionsGranted() { for(String permission : REQUIRED_PERMISSIONS) { if(ContextCompat.checkSelfPermission(this, permission)!= PackageManager.PERMISSION_GRANTED) { return false; } } return true; } private boolean checkCameraHardware(Context context) { return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA); } private void toggleFrontBackCamera() { } }

5.在布局文件里面添加

6.运行APP,因为模拟器没有前视摄像头,上真机调试就可以看到效果。 在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻


点击排行

实验室常用的仪器、试剂和
说到实验室常用到的东西,主要就分为仪器、试剂和耗
不用再找了,全球10大实验
01、赛默飞世尔科技(热电)Thermo Fisher Scientif
三代水柜的量产巅峰T-72坦
作者:寞寒最近,西边闹腾挺大,本来小寞以为忙完这
通风柜跟实验室通风系统有
说到通风柜跟实验室通风,不少人都纠结二者到底是不
集消毒杀菌、烘干收纳为一
厨房是家里细菌较多的地方,潮湿的环境、没有完全密
实验室设备之全钢实验台如
全钢实验台是实验室家具中较为重要的家具之一,很多

推荐新闻


图片新闻

实验室药品柜的特性有哪些
实验室药品柜是实验室家具的重要组成部分之一,主要
小学科学实验中有哪些教学
计算机 计算器 一般 打孔器 打气筒 仪器车 显微镜
实验室各种仪器原理动图讲
1.紫外分光光谱UV分析原理:吸收紫外光能量,引起分
高中化学常见仪器及实验装
1、可加热仪器:2、计量仪器:(1)仪器A的名称:量
微生物操作主要设备和器具
今天盘点一下微生物操作主要设备和器具,别嫌我啰嗦
浅谈通风柜使用基本常识
 众所周知,通风柜功能中最主要的就是排气功能。在

专题文章

    CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭