Android调用外置摄像头的方法
本文实例为大家分享了Android调用外置摄像头的具体代码,供大家参考,具体内容如下
1、布局文件
2、相应的MainActivity.java的主要代码如下
package com.deepreality.takephotowithusbcamera; import android.Manifest; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.SurfaceTexture; import android.hardware.Camera; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.TextureView; import android.view.View; import android.widget.ImageButton; import android.widget.Toast; import com.tbruyelle.rxpermissions2.RxPermissions; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener, View.OnClickListener { private static final String TAG = MainActivity.class.getSimpleName(); private Camera mCamera; private ImageButton mPlayButton; private RxPermissions rxPermissions; private int permissionNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rxPermissions = new RxPermissions(MainActivity.this); checkUserAllPermissions(); mPlayButton = (ImageButton) findViewById(R.id.play); mPlayButton.setOnClickListener(this); ((TextureView) findViewById(R.id.textureview)) .setSurfaceTextureListener(this); } private void takePic() { if (mCamera != null) { //调用抓拍摄像头抓拍 mCamera.takePicture(null, null, pictureCallback); } else { Log.e("TAG", "请检查摄像头!"); } } private Bitmap mBitmap; public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { Log.i("ygy", "onPictureTaken"); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式 System.out.println(df.format(new Date()));// new Date()为获取当前系统时间 String picName = df.format(new Date()); Toast.makeText(getApplicationContext(), "正在保存...", Toast.LENGTH_LONG).show(); mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); File file = new File("/storage/emulated/0/" + picName + ".jpg"); try { file.createNewFile(); BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file)); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); Toast.makeText(getApplicationContext(), "图像保存成功", Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); } } }; @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { mCamera = Camera.open(0); if (mCamera != null) { try { mCamera.setPreviewTexture(surface); mCamera.startPreview(); } catch (IOException e) { Log.d("TAG", e.getMessage()); } } } @Override protected void onStop() { if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } super.onStop(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { if (mCamera != null) { mCamera.stopPreview(); mCamera.release(); mCamera = null; } return false; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } @Override public void onClick(View v) { if (mCamera == null) { return; } takePic(); } /** * 检查并获取用户权限 */ private void checkUserAllPermissions() { rxPermissions .requestEach(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA ) .subscribe(permission -> { if (permission.granted) { } else if (permission.shouldShowRequestPermissionRationale) { } else {} permissionNum ++; if (permissionNum == 2) { } }); } }
3、注意在清单文件里AndroidManifest.xml添加用户权限
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
X 关闭
X 关闭
- 15G资费不大降!三大运营商谁提供的5G网速最快?中国信通院给出答案
- 2联想拯救者Y70发布最新预告:售价2970元起 迄今最便宜的骁龙8+旗舰
- 3亚马逊开始大规模推广掌纹支付技术 顾客可使用“挥手付”结账
- 4现代和起亚上半年出口20万辆新能源汽车同比增长30.6%
- 5如何让居民5分钟使用到各种设施?沙特“线性城市”来了
- 6AMD实现连续8个季度的增长 季度营收首次突破60亿美元利润更是翻倍
- 7转转集团发布2022年二季度手机行情报告:二手市场“飘香”
- 8充电宝100Wh等于多少毫安?铁路旅客禁止、限制携带和托运物品目录
- 9好消息!京东与腾讯续签三年战略合作协议 加强技术创新与供应链服务
- 10名创优品拟通过香港IPO全球发售4100万股 全球发售所得款项有什么用处?