zoukankan      html  css  js  c++  java
  • android如何实现后台拍照

    转载地址:http://www.apkbus.com/android-69728-2-1.html

    (1)CameraActivity.java

    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    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)// 拍照过后上传文件到服务器
    //  {
    //  }
     
    }
     
    3)camera_layout.xml
     
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ly"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
         
    </RelativeLayout>
  • 相关阅读:
    windows下区块链,私有链搭建详细教程(图文详解)
    MySQL 5.7 mysqldump的Bug导致复制异常
    关于MySQL 5.6 DDL阻塞DML的问题!
    mysqldump 根据时间字段导出数据的问题
    MySQL undo redo
    InnoDB undo, redo,binlog,data什么时候写?
    MySQL master 宕机导致slave数据比master多的case
    Jboss配置自动重连数据库
    星爷001正式开始写blog啦
    本地及远程二级缓存
  • 原文地址:https://www.cnblogs.com/garygou/p/2853127.html
Copyright © 2011-2022 走看看