android 相機(jī)使用(拍照树酪、錄像、圖片查看大州、靜態(tài)人臉識別续语、延時拍照)

相機(jī)是我們最常用的手機(jī)應(yīng)用之一,這里只是實(shí)現(xiàn)幾個簡單的功能厦画、拍照疮茄、延時拍照、錄像根暑、圖片查看力试、靜態(tài)人臉識別等。
1排嫌、這里主要使用surfaceView實(shí)現(xiàn)畸裳,首先打開攝像頭實(shí)現(xiàn)預(yù)覽,在surfaceCreated中添加

/**
     * 啟動預(yù)覽
     */
    private void startPreview() {
        if (mCamera != null || !mIsSurfaceCreated) {
            return;
        }
        mCamera = Camera.open(0);

        Camera.Parameters parameters = mCamera.getParameters();
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        Camera.Size size = getBestPreviewSize(width, height, parameters);

        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        parameters.setPreviewFrameRate(20);

        mCamera.setDisplayOrientation(90);
        mCamera.setParameters(parameters);

        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mCamera.startPreview();
    }

2淳地、實(shí)現(xiàn)預(yù)覽后躯畴,實(shí)現(xiàn)拍照要先實(shí)現(xiàn)Camera.PictureCallback接口,然后使用mCamera.takePicture(null, null, null, MainActivity.this);實(shí)現(xiàn)拍照薇芝,然后保存到手機(jī)上

@Override
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream fos = null;
        try {
            String fileName = System.currentTimeMillis() + ".png";
            fos = new FileOutputStream(new File(filePath + File.separator + fileName));
            videoPath = filePath + File.separator + fileName;
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            Matrix matrix = new Matrix();
            matrix.setRotate(90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            ivPicture.setImageBitmap(bitmap);
            new FaceThread(bitmap).start();
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Toast.makeText(getApplication(), "拍照成功", Toast.LENGTH_SHORT).show();
        //拍照后重新預(yù)覽
        mCamera.startPreview();
    }
關(guān)于延時拍照只需加個定時器就可以了具體需要延時多久可根據(jù)實(shí)際需要設(shè)置蓬抄,下面定時器代碼,
 Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            count++;
            countDown--;
            btnDelay.setText(countDown + " s");
            handler.postDelayed(this, 1000);
            if (countDown == 0) {
                mCamera.takePicture(null, null, null, MainActivity.this);
                btnDelay.setText("3s");
                handler.removeCallbacksAndMessages(null);
            }
            tvTime.setText(count + " s");

        }
    };

啟動定時器代碼夯到,當(dāng)點(diǎn)擊3s再點(diǎn)擊拍照就可以實(shí)現(xiàn)延時拍照功能

 handler.postDelayed(runnable, 1000);

3嚷缭、實(shí)現(xiàn)簡單攝像功能,主要使用mediaRecorder耍贾,錄像完保存到跟圖片同樣路徑下阅爽,代碼中有詳細(xì)注釋

/**
     * 錄像
     */
    private void videotape() {

        if (isVideoStart) {
            count = 0;
            btnDelay.setVisibility(View.GONE);
            tvTime.setVisibility(View.VISIBLE);
            handler.postDelayed(runnable, 1000);//每兩秒執(zhí)行一次runnable.

            btnVideo.setBackgroundResource(R.drawable.video_recoder);
            isVideoStart = false;
            mediarecorder = new MediaRecorder();
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
            if (mCamera != null) {
                mCamera.setDisplayOrientation(90);
                mCamera.unlock();
                mediarecorder.setCamera(mCamera);
            }
            //設(shè)置視頻源
            mediarecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
            //設(shè)置音頻源
            mediarecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            //設(shè)置文件輸出格式
            mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            //設(shè)置視頻編碼方式
            mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            //設(shè)置音頻編碼方式
            mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            //設(shè)置視頻高和寬,注意文檔的說明:
            //Must be called after setVideoSource().
            //Call this after setOutFormat() but before prepare().
            //設(shè)置錄制的視頻幀率,注意文檔的說明:
            //Must be called after setVideoSource().
            //Call this after setOutFormat() but before prepare().
            mediarecorder.setVideoSize(800, 480);
            mediarecorder.setVideoFrameRate(20);
            mediarecorder.setAudioChannels(2);
            //設(shè)置預(yù)覽畫面
            mediarecorder.setPreviewDisplay(mHolder.getSurface());
            //設(shè)置輸出路徑
            // videoPath = Environment.getExternalStorageDirectory() + File.separator + System.currentTimeMillis() + ".mp4";
            videoPath = "/mnt/sdcard/DCIM/Camera" + File.separator + System.currentTimeMillis() + ".mp4";
            mediarecorder.setOutputFile(videoPath);
            try {
                // 準(zhǔn)備錄制
                mediarecorder.prepare();
                // 開始錄制
                mediarecorder.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            tvTime.setText("");
            btnDelay.setVisibility(View.VISIBLE);
            btnDelay.setText("3 s");
            //ivPicture.setImageURI(Uri.fromFile(new File(videoPath)));
            ivPicture.setImageBitmap(getVideoThumbnail(videoPath));
            Log.e(TAG, "videoPath=" + videoPath);
            handler.removeCallbacksAndMessages(null);
            btnVideo.setBackgroundResource(R.drawable.ic_switch_video);
            isVideoStart = true;
            if (mediarecorder != null) {
                // 停止錄制
                mediarecorder.stop();
                // 釋放資源
                mediarecorder.release();
                mediarecorder = null;
            }

            if (mCamera != null) {
                // mCamera.release();
            }
            Toast.makeText(getApplication(), "視頻已保存到" + videoPath + "下", Toast.LENGTH_SHORT).show();
        }
    }

4、實(shí)現(xiàn)切換前后攝像頭

/**
     * 切換前后攝像頭
     */
    private void changeCamera() {
        int cameraCount = 0;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();//得到攝像頭的個數(shù)

        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);//得到每一個攝像頭的信息
            if (cameraPosition == 1) {
                //現(xiàn)在是后置荐开,變更為前置
                //代表攝像頭的方位付翁,CAMERA_FACING_FRONT  前置      CAMERA_FACING_BACK后置
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    mCamera.stopPreview();//停掉原來攝像頭的預(yù)覽
                    mCamera.release();//釋放資源
                    mCamera = null;//取消原來攝像頭
                    mCamera = Camera.open(i);//打開當(dāng)前選中的攝像頭
                    try {
                        mCamera.setPreviewDisplay(mHolder);//通過surfaceview顯示取景畫面
                        mCamera.setDisplayOrientation(90);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mCamera.startPreview();//開始預(yù)覽
                    cameraPosition = 0;
                    break;
                }
            } else {
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    mCamera.stopPreview();
                    mCamera.release();
                    mCamera = null;
                    mCamera = Camera.open(i);
                    try {
                        mCamera.setPreviewDisplay(mHolder);
                        mCamera.setDisplayOrientation(90);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mCamera.startPreview();
                    cameraPosition = 1;
                    break;
                }
            }
        }
    }

5、實(shí)現(xiàn)點(diǎn)擊屏幕出現(xiàn)方框聚焦功能晃听,首先我們先寫一個自定的view來繪制一個正方形

package com.example.apple.camera.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.example.apple.camera.R;


/**
 * 
 */

public class MyView extends View {

    private Paint mPaint;
    private Canvas mCanvas;
    private int x;
    private int y;
    private Bitmap mBitmap;

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mPaint = new Paint();
        mPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        mPaint.setColor(Color.BLUE);
        mPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.mCanvas = canvas;
        // setDrawRect(400,400);
        if (mBitmap==null){
            mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.border);
        }
        Bitmap mm = Bitmap.createScaledBitmap(mBitmap,200,200,true);
        canvas.drawBitmap(mm,x,y,mPaint);
    }
    public void setX(int x) {
        this.x = x;
        invalidate();
    }
    public void setY(int y) {
        this.y = y;
        invalidate();
    }

    public Bitmap getmBitmap() {
        return mBitmap;
    }

    public void setmBitmap(Bitmap mBitmap) {
        this.mBitmap = mBitmap;
        invalidate();
    }
    public void setDrawRect(int x, int y) {

        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.border);
        mCanvas.drawBitmap(mBitmap,x,y,mPaint);
    }
}

使用的時候就在onTouchEvent中實(shí)現(xiàn)百侧,首先要現(xiàn)在布局中隱藏,當(dāng)要使用的時候顯示出來能扒,再設(shè)置要顯示的位置佣渴,顯示位置通過觸摸屏幕當(dāng)前位置來獲取,然后當(dāng)聚焦實(shí)現(xiàn)后可以通過定時器來延時幾秒消失初斑,這里僅實(shí)現(xiàn)聚焦完后就隱藏辛润,這就簡單實(shí)現(xiàn)了點(diǎn)擊聚焦功能

@Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                focusOnTouch(event);
                myRect.setVisibility(View.VISIBLE);
                int x = (int) event.getX();
                int y = (int) event.getY();
                myRect.setX(x - 100);
                myRect.setY(y - 300);
                //Log.e(TAG,"x="+x+" y="+y);
                mCamera.autoFocus(new Camera.AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        if (success) {
                            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.border1);
                            myRect.setmBitmap(mBitmap);
                            myRect.setVisibility(View.GONE);
                        }
                    }
                });
                break;
        }

        return super.onTouchEvent(event);
    }

6、設(shè)置拍照圖片大小见秤,先獲取手機(jī)支持什么那些規(guī)格的砂竖,然后再設(shè)置真椿,否則可能會出錯,不同手機(jī)可能不一樣

List<Camera.Size> pszize = parameters.getSupportedPictureSizes();
        if (null != pszize && 0 < pszize.size()) {
            int height1[] = new int[pszize.size()];
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < pszize.size(); i++) {
                Camera.Size size1 = (Camera.Size) pszize.get(i);
                int sizeheight = size1.height;
                int sizewidth = size1.width;
                height1[i] = sizeheight;
                map.put(sizeheight, sizewidth);
                //Log.e(TAG,"size.width="+sizewidth + "  size.height="+sizeheight);
            }
            Arrays.sort(height1);
            parameters.setPictureSize(map.get(height1[12]), height1[12]);

如果報(bào)錯可以注釋掉設(shè)置預(yù)覽圖片大小和設(shè)置拍照圖片大小

 parameters.setPreviewSize(size.width, size.height);
//            parameters.setPictureSize(size.width, size.height);

7乎澄、關(guān)于設(shè)置錄像的清晰度和幀率可以設(shè)置下面參數(shù)

 mediarecorder.setVideoSize(800, 480);
            mediarecorder.setVideoFrameRate(20);

8突硝、實(shí)現(xiàn)簡單的人臉識別,這里只能靜態(tài)識別三圆,動態(tài)識別因?yàn)轭A(yù)覽回調(diào)回來的圖片太大狞换,無法處理避咆,可對著一張人臉拍照后可以預(yù)覽到實(shí)際效果舟肉,照片會存儲到手機(jī)上

class FaceThread extends Thread {
        private Bitmap mBitmap;

        public FaceThread(Bitmap bitmap) {
            this.mBitmap = bitmap;
        }

        @Override
        public void run() {

            FaceDetector.Face[] faces = new FaceDetector.Face[5];
            //格式必須為RGB_565才可以識別
            Bitmap bitmap = mBitmap.copy(Bitmap.Config.RGB_565, true);
            final int faceCount = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5).findFaces(bitmap, faces);
            bitmap.recycle();

            if (faceCount > 0) {
                final Bitmap bmp = parseBitmap(faces, faceCount, mBitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ivPicture.setImageBitmap(bmp);
                        File file = new File(filePath, System.currentTimeMillis() + ".jpg");
                        if (file.exists()) {
                            file.delete();
                        }
                        try {
                            FileOutputStream fo = new FileOutputStream(file);
                            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fo);
                            fo.flush();
                            fo.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }

        }
    }

    /**
     * 在人臉上畫矩形
     */
    private Bitmap parseBitmap(FaceDetector.Face[] faces, int faceCount, Bitmap mFaceBitmap) {
        Bitmap bitmap = Bitmap.createBitmap(mFaceBitmap.getWidth(), mFaceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);

        canvas.drawBitmap(mFaceBitmap, 0, 0, mPaint);
        for (int i = 0; i < faceCount; i++) {
            //雙眼的中心點(diǎn)
            PointF midPoint = new PointF();
            faces[i].getMidPoint(midPoint);
            //雙眼的距離
            float eyeDistance = faces[i].eyesDistance();
            //畫矩形
            canvas.drawRect(midPoint.x - eyeDistance, midPoint.y - eyeDistance, midPoint.x + eyeDistance, midPoint.y + eyeDistance, mPaint);
            myRect.setY(midPoint.x + eyeDistance);
            myRect.setX(midPoint.y + eyeDistance);
        }

        return bitmap;
    }

9、關(guān)于拍照后的圖片和錄制的視頻的顯示查库,通過路徑查找到所有的文件數(shù)據(jù)保存路媚,然后傳入顯示,具體可看代碼

/**
     * 查詢獲取圖片文件
     * @param path
     * @return
     */
    private ArrayList<String> queryImage(String path){
        ArrayList<String> filePath = new ArrayList<>();
        if (!TextUtils.isEmpty(path)){

            File file = new File(path);
            File[] files = file.listFiles();

            if (files!=null){
                if (!file.exists())
                    return null;

                for (File f : files){
                    if (getFileType(f)==IMAGE || getFileType(f) == VIDEO){
                        filePath.add(f.getPath());
                    }
                }
            }
        }
        return filePath;
    }

    public static int getFileType(File f) {
        int type = -1;
        String fileName = f.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".")
                + 1, fileName.length()).toLowerCase();
        if (ext.equals("mp3") || ext.equals("amr") || ext.equals("wma")
                || ext.equals("aac") || ext.equals("m4a") || ext.equals("mid")
                || ext.equals("xmf") || ext.equals("ogg") || ext.equals("wav")
                || ext.equals("qcp") || ext.equals("awb")|| ext.equals("3gpp")|| ext.equals("ape")|| ext.equals("flac")||ext.equals("midi")) {
            type = MUSIC;
        }
        else if (ext.equals("3gp") || ext.equals("avi") || ext.equals("mp4")
                || ext.equals("3g2") || ext.equals("wmv") || ext.equals("divx")
                || ext.equals("mkv") || ext.equals("webm") || ext.equals("ts")
                || ext.equals("asf")|| ext.equals("mov")||ext.equals("mpg")||ext.equals("flv") ) {
            type = VIDEO;
        }
        else if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("gif")
                || ext.equals("png") || ext.equals("bmp")||ext.equals("wbmp")) {
            type = IMAGE;
        }
        else if (ext.equals("doc") || ext.equals("docx") || ext.equals("xls")
                || ext.equals("xlsx") || ext.equals("ppt") || ext.equals("pptx")
                || ext.equals("txt") || ext.equals("text") || ext.equals("pdf")|| ext.equals("html")) {
            type = DOC;
        }
        else if (ext.equals("rar") || ext.equals("zip") || ext.equals("tar") || ext.equals("gz")
                || ext.equals("iso") || ext.equals("jar") || ext.equals("cab") || ext.equals("7z")
                || ext.equals("ace")) {
            type = ZIP;
        }
        else if (ext.equals("apk")) {
            type = APK;
        }
        else {
            type = OTHER;
        }
        return type;
    }

完整代碼

package com.example.apple.camera.showpicture;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.apple.camera.R;
import com.example.apple.camera.view.ZoomImageView;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by 550211 on 2017/9/7.
 */

public class ImageShowActivity extends Activity {

    private ViewPager mViewPager;
    private TextView tvFileCount;
    private ImageView ivPlay;

    public static final int IMAGE = 0;
    public static final int VIDEO = 4;
    public static final int MUSIC = 5;
    public static final int DOC = 6;
    public static final int ZIP=7;
    public static final int APK = 8;
    public static final int OTHER =9;

    private ArrayList<String> allImageFile = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_show);

        initView();
    }

    private void initView() {
        allImageFile = queryImage("/mnt/sdcard/DCIM/Camera");
        final ImageView[] mImageViews = new ImageView[allImageFile.size()];

        tvFileCount = (TextView)findViewById(R.id.tv_file_count);
        ivPlay = (ImageView)findViewById(R.id.iv_play);

        mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
        mViewPager.setAdapter(new PagerAdapter() {

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ZoomImageView imageView = new ZoomImageView(getApplicationContext());
                File file = new File(allImageFile.get(position));
                Glide.with(getApplication())
                        .load(file)
                        .centerCrop()
                        .into(imageView);
                container.addView(imageView);

                tvFileCount.setText(position+"/"+allImageFile.size());
                Log.e("ImageShowActivity","position="+file.getPath());
                mImageViews[position] = imageView;
                if (getFileType(file) == VIDEO){
                    ivPlay.setVisibility(View.VISIBLE);
                }else {
                    ivPlay.setVisibility(View.GONE);
                }
                return imageView;
            }

            @Override
            public void destroyItem(ViewGroup container, int position,
                                    Object object) {
                container.removeView(mImageViews[position]);
            }

            @Override
            public boolean isViewFromObject(View arg0, Object arg1) {
                return arg0 == arg1;
            }

            @Override
            public int getCount() {
                return allImageFile.size();
            }
        });
    }

    /**
     * 查詢獲取圖片文件
     * @param path
     * @return
     */
    private ArrayList<String> queryImage(String path){
        ArrayList<String> filePath = new ArrayList<>();
        if (!TextUtils.isEmpty(path)){

            File file = new File(path);
            File[] files = file.listFiles();

            if (files!=null){
                if (!file.exists())
                    return null;

                for (File f : files){
                    if (getFileType(f)==IMAGE || getFileType(f) == VIDEO){
                        filePath.add(f.getPath());
                    }
                }
            }
        }
        return filePath;
    }

    public static int getFileType(File f) {
        int type = -1;
        String fileName = f.getName();
        String ext = fileName.substring(fileName.lastIndexOf(".")
                + 1, fileName.length()).toLowerCase();
        if (ext.equals("mp3") || ext.equals("amr") || ext.equals("wma")
                || ext.equals("aac") || ext.equals("m4a") || ext.equals("mid")
                || ext.equals("xmf") || ext.equals("ogg") || ext.equals("wav")
                || ext.equals("qcp") || ext.equals("awb")|| ext.equals("3gpp")|| ext.equals("ape")|| ext.equals("flac")||ext.equals("midi")) {
            type = MUSIC;
        }
        else if (ext.equals("3gp") || ext.equals("avi") || ext.equals("mp4")
                || ext.equals("3g2") || ext.equals("wmv") || ext.equals("divx")
                || ext.equals("mkv") || ext.equals("webm") || ext.equals("ts")
                || ext.equals("asf")|| ext.equals("mov")||ext.equals("mpg")||ext.equals("flv") ) {
            type = VIDEO;
        }
        else if (ext.equals("jpg") || ext.equals("jpeg") || ext.equals("gif")
                || ext.equals("png") || ext.equals("bmp")||ext.equals("wbmp")) {
            type = IMAGE;
        }
        else if (ext.equals("doc") || ext.equals("docx") || ext.equals("xls")
                || ext.equals("xlsx") || ext.equals("ppt") || ext.equals("pptx")
                || ext.equals("txt") || ext.equals("text") || ext.equals("pdf")|| ext.equals("html")) {
            type = DOC;
        }
        else if (ext.equals("rar") || ext.equals("zip") || ext.equals("tar") || ext.equals("gz")
                || ext.equals("iso") || ext.equals("jar") || ext.equals("cab") || ext.equals("7z")
                || ext.equals("ace")) {
            type = ZIP;
        }
        else if (ext.equals("apk")) {
            type = APK;
        }
        else {
            type = OTHER;
        }
        return type;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

布局實(shí)現(xiàn)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_file_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/id_viewpager"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"></android.support.v4.view.ViewPager>

        <ImageView
            android:id="@+id/iv_play"
            android:visibility="gone"
            android:layout_centerInParent="true"
            android:background="@drawable/ic_gallery_play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RelativeLayout>
</LinearLayout>

圖片顯示view代碼

package com.example.apple.camera.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Matrix;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

/**
 * @author zhy
 *         博客地址:http://blog.csdn.net/lmj623565791
 */
public class ZoomImageView extends ImageView implements OnScaleGestureListener,
        OnTouchListener, ViewTreeObserver.OnGlobalLayoutListener

{
    private static final String TAG = ZoomImageView.class.getSimpleName();
    public static final float SCALE_MAX = 4.0f;
    private static final float SCALE_MID = 2.0f;
    private float initScale = 1.0f;
    private boolean once = true;
    private final float[] matrixValues = new float[9];
    private ScaleGestureDetector mScaleGestureDetector = null;
    private final Matrix mScaleMatrix = new Matrix();
    private GestureDetector mGestureDetector;
    private boolean isAutoScale;

    private int mTouchSlop;

    private float mLastX;
    private float mLastY;

    private boolean isCanDrag;
    private int lastPointerCount;

    private boolean isCheckTopAndBottom = true;
    private boolean isCheckLeftAndRight = true;

    public ZoomImageView(Context context) {
        this(context, null);
    }

    public ZoomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        super.setScaleType(ScaleType.MATRIX);
        mGestureDetector = new GestureDetector(context,
                new SimpleOnGestureListener() {
                    @Override
                    public boolean onDoubleTap(MotionEvent e) {
                        if (isAutoScale == true)
                            return true;

                        float x = e.getX();
                        float y = e.getY();
                        Log.e("DoubleTap", getScale() + " , " + initScale);

                        if (getScale() < SCALE_MID) {
                            ZoomImageView.this.postDelayed(new AutoScaleRunnable(SCALE_MID, x, y), 16);
                            isAutoScale = true;

                        } else if (getScale() >= SCALE_MID && getScale() < SCALE_MAX) {
                            ZoomImageView.this.postDelayed(new AutoScaleRunnable(SCALE_MAX, x, y), 16);
                            isAutoScale = true;

                        } else {
                            ZoomImageView.this.postDelayed(new AutoScaleRunnable(initScale, x, y), 16);
                            isAutoScale = true;
                        }

                        return true;
                    }
                });
        mScaleGestureDetector = new ScaleGestureDetector(context, this);
        this.setOnTouchListener(this);
    }

    /**
     * 自動縮放的任務(wù)
     *
     * @author zhy
     */
    private class AutoScaleRunnable implements Runnable {
        static final float BIGGER = 1.07f;
        static final float SMALLER = 0.93f;
        private float mTargetScale;
        private float tmpScale;

        /**
         * 縮放的中心
         */
        private float x;
        private float y;

        /**
         * 傳入目標(biāo)縮放值樊销,根據(jù)目標(biāo)值與當(dāng)前值整慎,判斷應(yīng)該放大還是縮小
         *
         * @param targetScale
         */
        public AutoScaleRunnable(float targetScale, float x, float y) {
            this.mTargetScale = targetScale;
            this.x = x;
            this.y = y;
            if (getScale() < mTargetScale) {
                tmpScale = BIGGER;
            } else {
                tmpScale = SMALLER;
            }

        }

        @Override
        public void run() {
            // 進(jìn)行縮放
            mScaleMatrix.postScale(tmpScale, tmpScale, x, y);
            checkBorderAndCenterWhenScale();
            setImageMatrix(mScaleMatrix);

            final float currentScale = getScale();
            // 如果值在合法范圍內(nèi),繼續(xù)縮放
            if (((tmpScale > 1f) && (currentScale < mTargetScale))
                    || ((tmpScale < 1f) && (mTargetScale < currentScale))) {
                ZoomImageView.this.postDelayed(this, 16);
            } else
            // 設(shè)置為目標(biāo)的縮放比例
            {
                final float deltaScale = mTargetScale / currentScale;
                mScaleMatrix.postScale(deltaScale, deltaScale, x, y);
                checkBorderAndCenterWhenScale();
                setImageMatrix(mScaleMatrix);
                isAutoScale = false;
            }

        }
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float scale = getScale();
        float scaleFactor = detector.getScaleFactor();

        if (getDrawable() == null)
            return true;

        /**
         * 縮放的范圍控制
         */
        if ((scale < SCALE_MAX && scaleFactor > 1.0f)
                || (scale > initScale && scaleFactor < 1.0f)) {
            /**
             * 最大值最小值判斷
             */
            if (scaleFactor * scale < initScale) {
                scaleFactor = initScale / scale;
            }
            if (scaleFactor * scale > SCALE_MAX) {
                scaleFactor = SCALE_MAX / scale;
            }
            /**
             * 設(shè)置縮放比例
             */
            mScaleMatrix.postScale(scaleFactor, scaleFactor, detector.getFocusX(), detector.getFocusY());
            checkBorderAndCenterWhenScale();
            setImageMatrix(mScaleMatrix);
        }
        return true;

    }

    /**
     * 在縮放時围苫,進(jìn)行圖片顯示范圍的控制
     */
    private void checkBorderAndCenterWhenScale() {

        RectF rect = getMatrixRectF();
        float deltaX = 0;
        float deltaY = 0;

        int width = getWidth();
        int height = getHeight();

        // 如果寬或高大于屏幕裤园,則控制范圍
        if (rect.width() >= width) {
            if (rect.left > 0) {
                deltaX = -rect.left;
            }
            if (rect.right < width) {
                deltaX = width - rect.right;
            }
        }
        if (rect.height() >= height) {
            if (rect.top > 0) {
                deltaY = -rect.top;
            }
            if (rect.bottom < height) {
                deltaY = height - rect.bottom;
            }
        }
        // 如果寬或高小于屏幕,則讓其居中
        if (rect.width() < width) {
            deltaX = width * 0.5f - rect.right + 0.5f * rect.width();
        }
        if (rect.height() < height) {
            deltaY = height * 0.5f - rect.bottom + 0.5f * rect.height();
        }
        Log.e(TAG, "deltaX = " + deltaX + " , deltaY = " + deltaY);

        mScaleMatrix.postTranslate(deltaX, deltaY);

    }

    /**
     * 根據(jù)當(dāng)前圖片的Matrix獲得圖片的范圍
     *
     * @return
     */
    private RectF getMatrixRectF() {
        Matrix matrix = mScaleMatrix;
        RectF rect = new RectF();
        Drawable d = getDrawable();
        if (null != d) {
            rect.set(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            matrix.mapRect(rect);
        }
        return rect;
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        return true;
    }

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (mGestureDetector.onTouchEvent(event))
            return true;
        mScaleGestureDetector.onTouchEvent(event);

        float x = 0, y = 0;
        // 拿到觸摸點(diǎn)的個數(shù)
        final int pointerCount = event.getPointerCount();
        // 得到多個觸摸點(diǎn)的x與y均值
        for (int i = 0; i < pointerCount; i++) {
            x += event.getX(i);
            y += event.getY(i);
        }
        x = x / pointerCount;
        y = y / pointerCount;

        /**
         * 每當(dāng)觸摸點(diǎn)發(fā)生變化時剂府,重置mLasX , mLastY
         */
        if (pointerCount != lastPointerCount) {
            isCanDrag = false;
            mLastX = x;
            mLastY = y;
        }

        lastPointerCount = pointerCount;
        RectF rectF = getMatrixRectF();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (rectF.width() > getWidth() || rectF.height() > getHeight()) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                if (rectF.width() > getWidth() || rectF.height() > getHeight()) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                }
                Log.e(TAG, "ACTION_MOVE");
                float dx = x - mLastX;
                float dy = y - mLastY;

                if (!isCanDrag) {
                    isCanDrag = isCanDrag(dx, dy);
                }
                if (isCanDrag) {

                    if (getDrawable() != null) {
                        isCheckLeftAndRight = isCheckTopAndBottom = true;
                        // 如果寬度小于屏幕寬度拧揽,則禁止左右移動
                        if (rectF.width() < getWidth()) {
                            dx = 0;
                            isCheckLeftAndRight = false;
                        }
                        // 如果高度小雨屏幕高度,則禁止上下移動
                        if (rectF.height() < getHeight()) {
                            dy = 0;
                            isCheckTopAndBottom = false;
                        }


                        mScaleMatrix.postTranslate(dx, dy);
                        checkMatrixBounds();
                        setImageMatrix(mScaleMatrix);
                    }
                }
                mLastX = x;
                mLastY = y;
                break;

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                Log.e(TAG, "ACTION_UP");
                lastPointerCount = 0;
                break;
        }

        return true;
    }

    /**
     * 獲得當(dāng)前的縮放比例
     *
     * @return
     */
    public final float getScale() {
        mScaleMatrix.getValues(matrixValues);
        return matrixValues[Matrix.MSCALE_X];
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        getViewTreeObserver().removeGlobalOnLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        if (once) {
            Drawable d = getDrawable();
            if (d == null)
                return;
            Log.e(TAG, d.getIntrinsicWidth() + " , " + d.getIntrinsicHeight());
            int width = getWidth();
            int height = getHeight();
            // 拿到圖片的寬和高
            int dw = d.getIntrinsicWidth();
            int dh = d.getIntrinsicHeight();
            float scale = 1.0f;
            // 如果圖片的寬或者高大于屏幕腺占,則縮放至屏幕的寬或者高
            if (dw > width && dh <= height) {
                scale = width * 1.0f / dw;
            }
            if (dh > height && dw <= width) {
                scale = height * 1.0f / dh;
            }
            // 如果寬和高都大于屏幕淤袜,則讓其按按比例適應(yīng)屏幕大小
            if (dw > width && dh > height) {
                scale = Math.min(width * 1.0f / dw, height * 1.0f / dh);
            }
            initScale = scale;

            Log.e(TAG, "initScale = " + initScale);
            mScaleMatrix.postTranslate((width - dw) / 2, (height - dh) / 2);
            mScaleMatrix.postScale(scale, scale, getWidth() / 2,
                    getHeight() / 2);
            // 圖片移動至屏幕中心
            setImageMatrix(mScaleMatrix);
            once = false;
        }
    }

    /**
     * 移動時,進(jìn)行邊界判斷衰伯,主要判斷寬或高大于屏幕的
     */
    private void checkMatrixBounds() {
        RectF rect = getMatrixRectF();

        float deltaX = 0, deltaY = 0;
        final float viewWidth = getWidth();
        final float viewHeight = getHeight();
        if (rect.top > 0 && isCheckTopAndBottom) {
            deltaY = -rect.top;
        }
        if (rect.bottom < viewHeight && isCheckTopAndBottom) {
            deltaY = viewHeight - rect.bottom;
        }
        if (rect.left > 0 && isCheckLeftAndRight) {
            deltaX = -rect.left;
        }
        if (rect.right < viewWidth && isCheckLeftAndRight) {
            deltaX = viewWidth - rect.right;
        }
        mScaleMatrix.postTranslate(deltaX, deltaY);
    }

    /**
     * 是否是推動行為
     *
     * @param dx
     * @param dy
     * @return
     */
    private boolean isCanDrag(float dx, float dy) {
        return Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop;
    }

}

相機(jī)顯示部分布局代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fresco="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_delay"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentRight="true"
            android:background="@android:color/transparent"
            android:padding="10dp"
            android:text="3s"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/btn_hdr"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_flash_off_indicator"
            android:padding="10dp" />

        <Button
            android:id="@+id/btn_camera_change"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_menu_revert_holo_dark"
            android:padding="10dp" />

        <Button
            android:id="@+id/btn_setting"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/ic_settings_normal"
            android:padding="10dp" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <SurfaceView
                android:id="@+id/surface_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1" />

            <com.example.apple.camera.view.MyView
                android:id="@+id/my_rect"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>

        <TextView
            android:id="@+id/tv_time"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textColor="@android:color/holo_red_dark" />


    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="10dp"
        android:gravity="center_vertical">

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="16dp"
            android:background="@mipmap/focus_ring_touch_outer" />

        <Button
            android:id="@+id/btn_video"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:background="@mipmap/ic_switch_video" />

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@mipmap/focus_ring_touch_outer" />

        <Button
            android:id="@+id/btn_take_photo"
            android:layout_width="51dp"
            android:layout_height="50dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:background="@mipmap/ic_video" />

        <com.facebook.drawee.view.SimpleDraweeView
            android:id="@+id/iv_picture"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:background="@mipmap/focus_ring_touch_outer"
            android:scaleType="centerCrop"

            fresco:actualImageScaleType="centerInside" />
    </RelativeLayout>


</LinearLayout>

完整相機(jī)功能代碼

package com.example.apple.camera;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.hardware.Camera;
import android.media.FaceDetector;
import android.media.MediaMetadataRetriever;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.apple.camera.showpicture.ImageShowActivity;
import com.example.apple.camera.view.MyView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static android.content.ContentValues.TAG;

public class MainActivity extends Activity implements View.OnClickListener, SurfaceHolder.Callback, Camera.PictureCallback {
    private Button btnVideo;
    private Button btnTakePhoto;
    private ImageView ivPicture;
    private SurfaceView mySurfaceView;
    private TextView tvTime;
    private Button btnDelay;
    private Button btnHdr;
    private Button btnCameraChange;
    private Button btnSetting;
    private MyView myRect = null;

    private String filePath = "/mnt/sdcard/DCIM/Camera";
    private Camera mCamera;
    private SurfaceHolder mHolder;
    private boolean mIsSurfaceCreated = false;

    private boolean isVideoStart = true;
    private String videoPath = "";
    private MediaRecorder mediarecorder;// 錄制視頻的類
    private int count = 5;
    private int countDown = 0;
    private boolean startDelayPicture = false;

    private int cameraPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView() {
        btnVideo = (Button) findViewById(R.id.btn_video);
        btnVideo.setOnClickListener(this);
        btnTakePhoto = (Button) findViewById(R.id.btn_take_photo);
        btnTakePhoto.setOnClickListener(this);
        ivPicture = (ImageView) findViewById(R.id.iv_picture);
        ivPicture.setOnClickListener(this);

        btnDelay = (Button) findViewById(R.id.btn_delay);
        btnDelay.setOnClickListener(this);
        btnHdr = (Button) findViewById(R.id.btn_hdr);
        btnHdr.setOnClickListener(this);
        btnCameraChange = (Button) findViewById(R.id.btn_camera_change);
        btnCameraChange.setOnClickListener(this);
        btnSetting = (Button) findViewById(R.id.btn_setting);
        btnSetting.setOnClickListener(this);
        myRect = (MyView) findViewById(R.id.my_rect);
        tvTime = (TextView) findViewById(R.id.tv_time);
        mySurfaceView = (SurfaceView) findViewById(R.id.surface_view);
        mHolder = mySurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.btn_camera_change:
                changeCamera();
                break;
            case R.id.btn_video:
                videotape();
                break;

            case R.id.btn_take_photo:
                if (startDelayPicture) {
                    handler.postDelayed(runnable, 1000);//每兩秒執(zhí)行一次runnable.
                    startDelayPicture = false;
                } else {
                    mCamera.takePicture(null, null, null, MainActivity.this);
                }
                break;
            case R.id.iv_picture:
                Intent intent = new Intent(MainActivity.this, ImageShowActivity.class);
                startActivity(intent);
                break;

            case R.id.btn_delay:
                countDown = 3;
                startDelayPicture = true;
                tvTime.setVisibility(View.GONE);
                break;

            case R.id.btn_setting:
                Intent intentSetting = new Intent(MainActivity.this, SettingActivity.class);
                startActivity(intentSetting);
                break;

            case R.id.btn_hdr:

                break;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:
                focusOnTouch(event);
                myRect.setVisibility(View.VISIBLE);
                int x = (int) event.getX();
                int y = (int) event.getY();
                myRect.setX(x - 100);
                myRect.setY(y - 300);
                //Log.e(TAG,"x="+x+" y="+y);
                mCamera.autoFocus(new Camera.AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        if (success) {
                            Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.border1);
                            myRect.setmBitmap(mBitmap);
                            myRect.setVisibility(View.GONE);
                        }
                    }
                });
                break;
        }

        return super.onTouchEvent(event);
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        FileOutputStream fos = null;
        try {
            String fileName = System.currentTimeMillis() + ".png";
            fos = new FileOutputStream(new File(filePath + File.separator + fileName));
            videoPath = filePath + File.separator + fileName;
            Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            Matrix matrix = new Matrix();
            matrix.setRotate(90);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            ivPicture.setImageBitmap(bitmap);
            new FaceThread(bitmap).start();
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Toast.makeText(getApplication(), "拍照成功", Toast.LENGTH_SHORT).show();
        //拍照后重新預(yù)覽
        mCamera.startPreview();
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mIsSurfaceCreated = true;
        startPreview();
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, final int width, final int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mIsSurfaceCreated = false;
        stopPreview();
    }

    /**
     * 啟動預(yù)覽
     */
    private void startPreview() {
        if (mCamera != null || !mIsSurfaceCreated) {
            return;
        }
        mCamera = Camera.open(0);

        Camera.Parameters parameters = mCamera.getParameters();
        int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        Camera.Size size = getBestPreviewSize(width, height, parameters);
        List<Camera.Size> pszize = parameters.getSupportedPictureSizes();
        if (null != pszize && 0 < pszize.size()) {
            int height1[] = new int[pszize.size()];
            Map<Integer, Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < pszize.size(); i++) {
                Camera.Size size1 = (Camera.Size) pszize.get(i);
                int sizeheight = size1.height;
                int sizewidth = size1.width;
                height1[i] = sizeheight;
                map.put(sizeheight, sizewidth);
                //Log.e(TAG,"size.width="+sizewidth + "  size.height="+sizeheight);
            }
            Arrays.sort(height1);
            parameters.setPictureSize(map.get(height1[12]), height1[12]);
//            if (Utils.getPictureSize(getApplication())!=0){
//                parameters.setPictureSize(map.get(height1[Utils.getPictureSize(getApplication())]), height1[Utils.getPictureSize(getApplication())]);
//                Log.e(TAG,"size.width="+map.get(height1[Utils.getPictureSize(getApplication())]) + "  size.height="+height1[Utils.getPictureSize(getApplication())]);
//            }else {
//                parameters.setPictureSize(size.width, size.height);
//            }
        }

        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        parameters.setPreviewFrameRate(20);

        mCamera.setDisplayOrientation(90);
        mCamera.setParameters(parameters);

        try {
            mCamera.setPreviewDisplay(mHolder);
        } catch (Exception e) {
            e.printStackTrace();
        }

        mCamera.startPreview();
    }

    /**
     * 關(guān)閉預(yù)覽
     */
    private void stopPreview() {
        if (mCamera != null) {
            try {
                mCamera.setPreviewDisplay(null);
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            } catch (Exception e) {
                Log.e(TAG, e.getMessage());
            }
        }
    }

    private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
        Camera.Size result = null;

        for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
            if (size.width <= width && size.height <= height) {
                if (result == null) {
                    result = size;
                } else {
                    int resultArea = result.width * result.height;
                    int newArea = size.width * size.height;

                    if (newArea > resultArea) {
                        result = size;
                    }
                }
            }
        }

        return result;
    }

    /**
     * 觸摸聚焦
     *
     * @param event
     */
    public void focusOnTouch(MotionEvent event) {
        int[] location = new int[2];
        mySurfaceView.getLocationOnScreen(location);
        Rect focusRect = calculateTapArea(100, 100, 1f, event.getRawX(), event.getRawY(),
                location[0], location[0] + mySurfaceView.getWidth(), location[1], location[1] + mySurfaceView.getHeight());
        Rect meteringRect = calculateTapArea(200, 200, 1.5f, event.getRawX(), event.getRawY(),
                location[0], location[0] + mySurfaceView.getWidth(), location[1], location[1] + mySurfaceView.getHeight());

        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        if (parameters.getMaxNumFocusAreas() > 0) {
            List<Camera.Area> focusAreas = new ArrayList<Camera.Area>();
            focusAreas.add(new Camera.Area(focusRect, 1000));
            parameters.setFocusAreas(focusAreas);
        }
        if (parameters.getMaxNumMeteringAreas() > 0) {
            List<Camera.Area> meteringAreas = new ArrayList<Camera.Area>();
            meteringAreas.add(new Camera.Area(meteringRect, 1000));
            parameters.setMeteringAreas(meteringAreas);
        }

        try {
            mCamera.setParameters(parameters);
        } catch (Exception e) {
        }
        mCamera.autoFocus(null);
    }

    public Rect calculateTapArea(int focusWidth, int focusHeight, float areaMultiple,
                                 float x, float y, int previewleft, int previewRight, int previewTop, int previewBottom) {
        int areaWidth = (int) (focusWidth * areaMultiple);
        int areaHeight = (int) (focusHeight * areaMultiple);
        int centerX = (previewleft + previewRight) / 2;
        int centerY = (previewTop + previewBottom) / 2;
        double unitx = ((double) previewRight - (double) previewleft) / 2000;
        double unity = ((double) previewBottom - (double) previewTop) / 2000;
        int left = clamp((int) (((x - areaWidth / 2) - centerX) / unitx), -1000, 1000);
        int top = clamp((int) (((y - areaHeight / 2) - centerY) / unity), -1000, 1000);
        int right = clamp((int) (left + areaWidth / unitx), -1000, 1000);
        int bottom = clamp((int) (top + areaHeight / unity), -1000, 1000);

        return new Rect(left, top, right, bottom);
    }

    public int clamp(int x, int min, int max) {
        if (x > max)
            return max;
        if (x < min)
            return min;
        return x;
    }

    /**
     * 錄像
     */
    private void videotape() {

        if (isVideoStart) {
            count = 0;
            btnDelay.setVisibility(View.GONE);
            tvTime.setVisibility(View.VISIBLE);
            handler.postDelayed(runnable, 1000);//每兩秒執(zhí)行一次runnable.

            btnVideo.setBackgroundResource(R.drawable.video_recoder);
            isVideoStart = false;
            mediarecorder = new MediaRecorder();
            mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
            if (mCamera != null) {
                mCamera.setDisplayOrientation(90);
                mCamera.unlock();
                mediarecorder.setCamera(mCamera);
            }
            //設(shè)置視頻源
            mediarecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
            //設(shè)置音頻源
            mediarecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
            //設(shè)置文件輸出格式
            mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            //設(shè)置視頻編碼方式
            mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            //設(shè)置音頻編碼方式
            mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
            //設(shè)置視頻高和寬,注意文檔的說明:
            //Must be called after setVideoSource().
            //Call this after setOutFormat() but before prepare().
            //設(shè)置錄制的視頻幀率,注意文檔的說明:
            //Must be called after setVideoSource().
            //Call this after setOutFormat() but before prepare().
            mediarecorder.setVideoSize(800, 480);
            mediarecorder.setVideoFrameRate(20);
            mediarecorder.setAudioChannels(2);
            //設(shè)置預(yù)覽畫面
            mediarecorder.setPreviewDisplay(mHolder.getSurface());
            //設(shè)置輸出路徑
            // videoPath = Environment.getExternalStorageDirectory() + File.separator + System.currentTimeMillis() + ".mp4";
            videoPath = "/mnt/sdcard/DCIM/Camera" + File.separator + System.currentTimeMillis() + ".mp4";
            mediarecorder.setOutputFile(videoPath);
            try {
                // 準(zhǔn)備錄制
                mediarecorder.prepare();
                // 開始錄制
                mediarecorder.start();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            tvTime.setText("");
            btnDelay.setVisibility(View.VISIBLE);
            btnDelay.setText("3 s");
            //ivPicture.setImageURI(Uri.fromFile(new File(videoPath)));
            ivPicture.setImageBitmap(getVideoThumbnail(videoPath));
            Log.e(TAG, "videoPath=" + videoPath);
            handler.removeCallbacksAndMessages(null);
            btnVideo.setBackgroundResource(R.drawable.ic_switch_video);
            isVideoStart = true;
            if (mediarecorder != null) {
                // 停止錄制
                mediarecorder.stop();
                // 釋放資源
                mediarecorder.release();
                mediarecorder = null;
            }

            if (mCamera != null) {
                // mCamera.release();
            }
            Toast.makeText(getApplication(), "視頻已保存到" + videoPath + "下", Toast.LENGTH_SHORT).show();
        }
    }

    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            try {
                retriever.release();
            } catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            count++;
            countDown--;
            btnDelay.setText(countDown + " s");
            handler.postDelayed(this, 1000);
            if (countDown == 0) {
                mCamera.takePicture(null, null, null, MainActivity.this);
                btnDelay.setText("3s");
                handler.removeCallbacksAndMessages(null);
            }
            tvTime.setText(count + " s");

        }
    };

    /**
     * 切換前后攝像頭
     */
    private void changeCamera() {
        int cameraCount = 0;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();//得到攝像頭的個數(shù)

        for (int i = 0; i < cameraCount; i++) {
            Camera.getCameraInfo(i, cameraInfo);//得到每一個攝像頭的信息
            if (cameraPosition == 1) {
                //現(xiàn)在是后置铡羡,變更為前置
                //代表攝像頭的方位,CAMERA_FACING_FRONT  前置      CAMERA_FACING_BACK后置
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    mCamera.stopPreview();//停掉原來攝像頭的預(yù)覽
                    mCamera.release();//釋放資源
                    mCamera = null;//取消原來攝像頭
                    mCamera = Camera.open(i);//打開當(dāng)前選中的攝像頭
                    try {
                        mCamera.setPreviewDisplay(mHolder);//通過surfaceview顯示取景畫面
                        mCamera.setDisplayOrientation(90);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mCamera.startPreview();//開始預(yù)覽
                    cameraPosition = 0;
                    break;
                }
            } else {
                if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                    mCamera.stopPreview();
                    mCamera.release();
                    mCamera = null;
                    mCamera = Camera.open(i);
                    try {
                        mCamera.setPreviewDisplay(mHolder);
                        mCamera.setDisplayOrientation(90);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    mCamera.startPreview();
                    cameraPosition = 1;
                    break;
                }
            }
        }
    }
    class FaceThread extends Thread {
        private Bitmap mBitmap;
        public FaceThread(Bitmap bitmap) {
            this.mBitmap = bitmap;
        }
        @Override
        public void run() {
            FaceDetector.Face[] faces = new FaceDetector.Face[5];
            //格式必須為RGB_565才可以識別
            Bitmap bitmap = mBitmap.copy(Bitmap.Config.RGB_565, true);
            final int faceCount = new FaceDetector(bitmap.getWidth(), bitmap.getHeight(), 5).findFaces(bitmap, faces);
            bitmap.recycle();

            if (faceCount > 0) {
                final Bitmap bmp = parseBitmap(faces, faceCount, mBitmap);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ivPicture.setImageBitmap(bmp);
                        File file = new File(filePath, System.currentTimeMillis() + ".jpg");
                        if (file.exists()) {
                            file.delete();
                        }
                        try {
                            FileOutputStream fo = new FileOutputStream(file);
                            bmp.compress(Bitmap.CompressFormat.JPEG, 90, fo);
                            fo.flush();
                            fo.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        }
    }

    /**
     * 在人臉上畫矩形
     */
    private Bitmap parseBitmap(FaceDetector.Face[] faces, int faceCount, Bitmap mFaceBitmap) {
        Bitmap bitmap = Bitmap.createBitmap(mFaceBitmap.getWidth(), mFaceBitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.YELLOW);
        mPaint.setStrokeWidth(5);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawBitmap(mFaceBitmap, 0, 0, mPaint);
        for (int i = 0; i < faceCount; i++) {
            //雙眼的中心點(diǎn)
            PointF midPoint = new PointF();
            faces[i].getMidPoint(midPoint);
            //雙眼的距離
            float eyeDistance = faces[i].eyesDistance();
            //畫矩形
            canvas.drawRect(midPoint.x - eyeDistance, midPoint.y - eyeDistance, midPoint.x + eyeDistance, midPoint.y + eyeDistance, mPaint);
            myRect.setY(midPoint.x + eyeDistance);
            myRect.setX(midPoint.y + eyeDistance);
        }
        return bitmap;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
            handler = null;
        }
    }
}
8242E730-91BC-43C8-8D05-7F3E87CA540F.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末意鲸,一起剝皮案震驚了整個濱河市烦周,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌怎顾,老刑警劉巖论矾,帶你破解...
    沈念sama閱讀 210,978評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異杆勇,居然都是意外死亡贪壳,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,954評論 2 384
  • 文/潘曉璐 我一進(jìn)店門蚜退,熙熙樓的掌柜王于貴愁眉苦臉地迎上來闰靴,“玉大人彪笼,你說我怎么就攤上這事÷烨遥” “怎么了配猫?”我有些...
    開封第一講書人閱讀 156,623評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長杏死。 經(jīng)常有香客問我泵肄,道長,這世上最難降的妖魔是什么淑翼? 我笑而不...
    開封第一講書人閱讀 56,324評論 1 282
  • 正文 為了忘掉前任腐巢,我火速辦了婚禮,結(jié)果婚禮上玄括,老公的妹妹穿的比我還像新娘冯丙。我一直安慰自己,他們只是感情好遭京,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,390評論 5 384
  • 文/花漫 我一把揭開白布胃惜。 她就那樣靜靜地躺著,像睡著了一般哪雕。 火紅的嫁衣襯著肌膚如雪船殉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,741評論 1 289
  • 那天斯嚎,我揣著相機(jī)與錄音利虫,去河邊找鬼。 笑死孝扛,一個胖子當(dāng)著我的面吹牛列吼,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苦始,決...
    沈念sama閱讀 38,892評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼寞钥,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了陌选?” 一聲冷哼從身側(cè)響起理郑,我...
    開封第一講書人閱讀 37,655評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎咨油,沒想到半個月后您炉,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,104評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡役电,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評論 2 325
  • 正文 我和宋清朗相戀三年赚爵,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,569評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡冀膝,死狀恐怖唁奢,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情窝剖,我是刑警寧澤麻掸,帶...
    沈念sama閱讀 34,254評論 4 328
  • 正文 年R本政府宣布,位于F島的核電站赐纱,受9級特大地震影響脊奋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜疙描,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,834評論 3 312
  • 文/蒙蒙 一诚隙、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧淫痰,春花似錦最楷、人聲如沸整份。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,725評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽烈评。三九已至火俄,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間讲冠,已是汗流浹背瓜客。 一陣腳步聲響...
    開封第一講書人閱讀 31,950評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竿开,地道東北人谱仪。 一個月前我還...
    沈念sama閱讀 46,260評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像否彩,于是被迫代替她去往敵國和親疯攒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,446評論 2 348

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 171,734評論 25 707
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫列荔、插件敬尺、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,058評論 4 62
  • 上一篇介紹了如何使用系統(tǒng)相機(jī)簡單、快速的進(jìn)行拍照贴浙,本篇將介紹如何使用框架提供的API直接控制攝像機(jī)硬件砂吞。 你還在為...
    Xiao_Mai閱讀 7,158評論 4 18
  • AngularJs框架的核心概念: MVC架構(gòu)模式(Model-View-Controller) 模型(Model...
    紋小艾閱讀 308評論 0 1
  • 那天下午,我去盲宣隊(duì)看望盲藝人紅權(quán)大哥崎溃,偶遇小盲童亮亮蜻直,經(jīng)過聊天,才知道亮亮來自呂梁市離石區(qū)的一個農(nóng)村,此次專程來...
    郝逗閱讀 402評論 0 1