如何在android中后台开启摄像头默默拍照

 我来答
百度网友ded4135
高粉答主

2018-02-01 · 醉心答题,欢迎关注
知道大有可为答主
回答量:2.7万
采纳率:87%
帮助的人:1.2亿
展开全部

(1)CameraActivity.java

package com.camera.test; import android.app.Activity;import android.content.pm.ActivityInfo;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;import android.widget.RelativeLayout; public class CameraActivity extends Activity {    private CameraView view;    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         getWindow().clearFlags(                WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);        requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置横屏模式以及全屏模式         view = new CameraView(this);// 通过一个surfaceview的view来实现拍照        view.setId(1);        view = new CameraView(this, this);        setContentView(R.layout.camera_layout);        RelativeLayout relative = (RelativeLayout) this.findViewById(R.id.ly);        RelativeLayout.LayoutParams Layout = new RelativeLayout.LayoutParams(3, 3);// 设置surfaceview使其满足需求无法观看预览        Layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);        Layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);         relative.addView(view, Layout);     } }   (2) CameraView.java package com.camera.test; import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import android.app.Activity;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Point;import android.graphics.PointF;import android.hardware.Camera;import android.hardware.Camera.AutoFocusCallback;import android.hardware.Camera.ShutterCallback;import android.media.AudioManager;import android.media.FaceDetector;import android.media.FaceDetector.Face;import android.os.Environment;import android.os.Handler; import android.view.SurfaceHolder;import android.view.SurfaceView;import android.widget.Toast; public class CameraView extends SurfaceView implements SurfaceHolder.Callback,        Camera.PictureCallback{     private SurfaceHolder holder;    private Camera camera;    private Camera.Parameters parameters;    private Activity act;    private Handler handler = new Handler();    private Context context;    private SurfaceView surfaceView;    private AudioManager audio;    private int current;     public CameraView(Context context) {        super(context);         surfaceView = this;        audio = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);        final int current = audio.getRingerMode();        audio.setRingerMode(AudioManager.RINGER_MODE_SILENT);        this.context = context;        holder = getHolder();// 生成Surface Holder        holder.addCallback(this);        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 指定Push Buffer         handler.postDelayed(new Runnable() {             @Override            public void run() {                if (camera == null) {                    handler.postDelayed(this, 1 * 1000);// 由于启动camera需要时间,在此让其等两秒再进行聚焦知道camera不为空                } else {                    camera.autoFocus(new AutoFocusCallback() {                        @Override                        public void onAutoFocus(boolean success, Camera camera) {                            if (success) {                                camera.takePicture(new ShutterCallback() {// 如果聚焦成功则进行拍照                                            @Override                                            public void onShutter() {                                            }                                        }, null, CameraView.this);                            } else {                            }                        }                    });                }            }        }, 2 * 1000);    }     public CameraView(Context context, Activity act) {// 在此定义一个构造方法用于拍照过后把CameraActivity给finish掉        this(context);        this.act = act;    }     @Override    public void surfaceCreated(final SurfaceHolder holder) {        // TODO Auto-generated method stub         camera = Camera.open();// 摄像头的初始化        handler.postDelayed(new Runnable() {             @Override            public void run() {                if (holder != null) {                    try {                        camera.setPreviewDisplay(holder);                    } catch (IOException e) {                        // TODO Auto-generated catch block                        e.printStackTrace();                    }                } else {                    handler.postDelayed(this, 1 * 1000);                }            }        }, 2 * 1000);    }     @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width,            int height) {        // TODO Auto-generated method stub         parameters = camera.getParameters();        camera.setParameters(parameters);// 设置参数        camera.startPreview();// 开始预览    }     @Override    public void surfaceDestroyed(SurfaceHolder holder) {        // TODO Auto-generated method stub     }     public void onPictureTaken(byte[] data, Camera camera) {// 拍摄完成后保存照片         try {             Date date = new Date();            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");            String time = format.format(date);             //在SD卡上创建文件夹            File file = new File(Environment.getExternalStorageDirectory()                    + "/myCamera/pic");            if (!file.exists()) {                 file.mkdirs();            }             String path = Environment.getExternalStorageDirectory()                    + "/myCamera/pic/" + time + ".jpg";            data2file(data, path);            camera.setPreviewCallback(null);            camera.stopPreview();            camera.release();            camera = null;            holder.removeCallback(CameraView.this);            audio.setRingerMode(current);            act.finish();            //uploadFile(path);         } catch (Exception e) {         }     }     private void data2file(byte[] w, String fileName) throws Exception {// 将二进制数据转换为文件的函数        FileOutputStream out = null;        try {            out = new FileOutputStream(fileName);            out.write(w);            out.close();        } catch (Exception e) {            if (out != null)                out.close();            throw e;        }    }//  private void uploadFile(String filePath)// 拍照过后上传文件到服务器//  {//  } } 

美狐美颜sdk
2024-08-06 广告
美颜SDK推出多个版本的原因有以下几点:1. 满足不同平台的需求:美颜SDK的不同版本可以满足不同平台的运营需求。例如,对于直播平台,需要更多的美颜功能来吸引观众,而视频聊天平台则需要的是精简版的美颜功能。通过提供不同版本的美颜SDK,可以... 点击进入详情页
本回答由美狐美颜sdk提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式