欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android 无预览拍照功能

时间:2021-02-13 11:14:48|栏目:Android代码|点击:

最近得到了一个需求,在后台拍照并保存

public void onTakePhotoClicked() {
   final SurfaceView preview = new SurfaceView(this);
   SurfaceHolder holder = preview.getHolder();
   // deprecated setting, but required on Android versions prior to 3.0
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
 holder.addCallback(new SurfaceHolder.Callback() {
     @Override
     //The preview must happen at or after this point or takePicture fails
     public void surfaceCreated(SurfaceHolder holder) {
       Log.d(TAG, "Surface created");
        camera = null;
       try {
         camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
         Log.d(TAG, "Opened camera");
         try {
           camera.setPreviewDisplay(holder);
         } catch (IOException e) {
           throw new RuntimeException(e);
         }
         camera.startPreview();
         Log.d(TAG, "Started preview");
      //延时拍照
         ThreadUtils.postOnUiThreadDelayed(new Runnable() {
           @Override
           public void run() {
             Log.e("zgj","开始拍照");
             camera.takePicture(null, null, CameraService.this);
           }
         },5000);
       } catch (Exception e) {
         if (camera != null)
           camera.release();
         throw new RuntimeException(e);
       }
     }
     @Override
     public void surfaceDestroyed(SurfaceHolder holder) {}
     @Override
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
   });
   WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
   WindowManager.LayoutParams params = new WindowManager.LayoutParams(
       1, 1, //Must be at least 1x1
       WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
       0,
       //Don't know if this is a safe default
       PixelFormat.UNKNOWN);
   //Don't set the preview visibility to GONE or INVISIBLE
   wm.addView(preview, params);
 }
 @Override
 public void onPictureTaken(byte[] bytes, Camera camera) {
   Log.e("zgj", "拍照结束");
   File pictureDir = Environment.getExternalStorageDirectory();
   if (pictureDir == null) {
     Log.d("zgj",
         "Error creating media file, check storage permissions!");
     return;
   }
   try {
     String pictureName = "ssss.png";
     File file = new File(pictureDir + "/pic/");
     if (!file.exists()) {
       file.mkdir();
     }
     file = new File(pictureDir + "/pic/" + pictureName);
     FileOutputStream fos = new FileOutputStream(file);
     fos.write(bytes);
     fos.close();
   } catch (FileNotFoundException e) {
     Log.d("zgj", "File not found: " + e.getMessage());
   } catch (IOException e) {
     Log.d("zgj", "Error accessing file: " + e.getMessage());
   }
 }

总结

上一篇:Android通过PHP服务器实现登录功能

栏    目:Android代码

下一篇:Android 自定义弹出框实现代码

本文标题:Android 无预览拍照功能

本文地址:http://www.codeinn.net/misctech/62428.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有