• 相机Camera


    Camera需与SurfaceView配合使用

    Camera类常用方法:

    Camera.open() 创建Camera实例,打开相机
    getParameters() 获取相机参数
    release() 释放相机资源
    setParameters(Camera.Parameters parameters) 设置相机参数
    setPreviewDisplay(SurfaceHolder holder) 设置用于预览的SurfaceView
    setDisplayOrientation(int i) 设置默认角度,值为度数,如值为90即90°
    startPreview() 开始预览
    stopPreview() 停止预览
    takePicture(Camera.ShutterCallback shutter,Camera.PictureCallback raw, Camera.PictureCallback jepg) 用于拍摄,参数shutter为快门回调函数常用于音效等;参数raw为拍摄后回调提供照片数据;参数jpeg为拍摄后回调提供无损照片数据
    

    相机参数常用方法

    setPictureSize(int width,int height)  设置图片尺寸
    setPictureFormat(int pixelFormat)  设置图片格式,如PixelFormat.JPEG
    set("jpeg-quality",int quality)  设置图片质量,quality值标准为65一般为75左右不超过100
    setFocusMode(String focusMode) 设置对焦模式,如FOCUS_MODE_CONTINUOUS_PICTURE为连续图片对焦
    1. //设置相机
    2. camera= Camera.open();
    3. Camera.Parameters parameters =camera.getParameters();
    4. parameters.setPictureSize(640*3,480*3);
    5. parameters.setPictureFormat(PixelFormat.JPEG);
    6. parameters.set("jpeg-quality",80);
    7. parameters.setFocusMode(FOCUS_MODE_CONTINUOUS_PICTURE);
    8. camera.setParameters(parameters);
    9. camera.setDisplayOrientation(90);
    1. //绑定SurfaceView
    2. SurfaceHolder surfaceHolder=surfaceView.getHolder();
    3. surfaceHolder.addCallback(new SurfaceHolder.Callback() {
    4. public void surfaceCreated(@NonNull SurfaceHolder surfaceHolder) {
    5. try {
    6. camera.setPreviewDisplay(surfaceHolder);
    7. //启动预览
    8. camera.startPreview();
    9. } catch (IOException e) {
    10. throw new RuntimeException(e);
    11. }
    12. }
    13. public void surfaceChanged(@NonNull SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    14. try {
    15. camera.setPreviewDisplay(surfaceHolder);
    16. //启动预览
    17. camera.startPreview();
    18. } catch (IOException e) {
    19. throw new RuntimeException(e);
    20. }
    21. }
    22. public void surfaceDestroyed(@NonNull SurfaceHolder surfaceHolder) {
    23. try {
    24. camera.setPreviewDisplay(surfaceHolder);
    25. //启动预览
    26. camera.startPreview();
    27. } catch (IOException e) {
    28. throw new RuntimeException(e);
    29. }
    30. }
    31. });
    32. //设置拍摄按钮监听器
    33. putButton.setOnClickListener(new View.OnClickListener() {
    34. public void onClick(View view) {
    35. camera.takePicture(null, null, new Camera.PictureCallback() {
    36. public void onPictureTaken(byte[] bytes, Camera camera) {
    37. camera.stopPreview();
    38. Bitmap bitmap_base=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
    39. String sdPath= String.valueOf(Environment.getExternalStorageDirectory());
    40. //API29以上已不允许在SD卡目录下创建文件夹或文件,需在已有(自带)文件夹下创建文件夹或文件
    41. File file=new File(sdPath+"/DCIM/myCamera");
    42. boolean fileExists=false;
    43. if(!file.exists()){
    44. fileExists=file.mkdirs();
    45. }
    46. else {
    47. fileExists=true;
    48. }
    49. Log.d("OK","文件夹是否存在"+fileExists);
    50. int num=1;
    51. while (true){
    52. String imageName="image"+num+".jpeg";
    53. File imageFile=new File(file.getAbsolutePath()+"/"+imageName);
    54. if(!imageFile.exists()){
    55. //不存在,进行创建
    56. imageFile.getParentFile().mkdirs();
    57. try {
    58. imageFile.createNewFile();
    59. } catch (IOException e) {
    60. throw new RuntimeException(e+"地址"+imageFile.getAbsolutePath());
    61. }
    62. try {
    63. FileOutputStream outputStream = new FileOutputStream(imageFile);
    64. bitmap_base.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
    65. Log.d("OK","保存完成"+imageFile.getAbsolutePath());
    66. outputStream.flush();
    67. outputStream.close();
    68. } catch (FileNotFoundException e) {
    69. throw new RuntimeException(e);
    70. } catch (IOException e) {
    71. throw new RuntimeException(e);
    72. }
    73. camera.startPreview();
    74. break;
    75. }
    76. else {
    77. num++;
    78. }
    79. }
    80. }
    81. });
    82. }
    83. });
    1. //要记得释放相机资源
    2. protected void onDestroy() {
    3. super.onDestroy();
    4. if(camera!=null){
    5. camera.release();
    6. }
    7. }
    8. protected void onPause() {
    9. super.onPause();
    10. camera.stopPreview();
    11. }
    12. protected void onRestart() {
    13. super.onRestart();
    14. camera.startPreview();
    15. }

  • 相关阅读:
    Linux常用操作命令大全
    笔试强训-day01_T1 BC153 [NOIP2010]数字统计
    2022最新版-李宏毅机器学习深度学习课程-P50 BERT的预训练和微调
    [iOS]-消息传递和消息转发机制
    Electron自动化测试技术选型调研
    Python输出字母在字符串中位置索引
    Linux--信号signal、父子进程、SIGCHLD信号相关命令
    TIA博途中如何在网络视图中显示完整的设备名称?
    常见限流算法学习
    离散数学(十二):关系的幂运算与关系的性质
  • 原文地址:https://blog.csdn.net/m0_57150356/article/details/133146785