`
dengyin2000
  • 浏览: 1208712 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android使用Camera截取身份证号

 
阅读更多
主要是利用Camera的拍照,画了几个框,然后Bitmap的裁剪。自动对焦成功5次之后自动拍照,其中处理照片放在了UI线程, 大家做的时候可以放到后端处理更好。

大家可以看看官方文档tranning和api guide里面关于Camera这块的文章,还有apiDemos里面的CameraPreview.java。看完基本上就能做一个独立的摄像头app了。

package com.example.myapp;
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.*;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.io.IOException;
import java.util.List;



// ----------------------------------------------------------------------

public class CameraPreview extends Activity implements Camera.PictureCallback {
    private Preview mPreview;
    Camera mCamera;
    int numberOfCameras;
    int cameraCurrentlyLocked;

    // The first rear facing camera
    int defaultCameraId;

    private Handler handler;

    private FrameLayout previewView;
    private ImageView resultImageView;

    public int previewWidth;
    public int previewHeight;
    public int cameraWidth;
    public int cameraHeight;

    public int autoFocusSuccessTimes;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                prepareCameraLayout();
            }
        };

        previewView = (FrameLayout) findViewById(R.id.preview);
        resultImageView = (ImageView) findViewById(R.id.result);
        // Create a RelativeLayout container that will hold a SurfaceView,
        // and set it as the content of our activity.
        mPreview = new Preview(this);
        mPreview.handler = this.handler;
        previewView.addView(mPreview);
        previewView.addView(new CardHintView(this));

        // Find the total number of cameras available
        numberOfCameras = Camera.getNumberOfCameras();

        // Find the ID of the default camera
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                defaultCameraId = i;
            }
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mCamera.autoFocus(autoFocusCallback);
            }
        }, 1000);// 每10毫秒连续聚焦
    }


    /**
     * 聚焦事件内部类
     */
    Camera.AutoFocusCallback autoFocusCallback = new Camera.AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean success, Camera camera) {//这里是连续聚焦算法

            Log.i("Camera", "success:" + success);
            if (success) {
                autoFocusSuccessTimes++;
            } else {
                autoFocusSuccessTimes = 0;
            }
            if (autoFocusSuccessTimes > 4) {
                Log.i("Camera", "auto focus 5 time");
                autoFocusSuccessTimes = 0;
                camera.takePicture(null, null, CameraPreview.this);
            } else {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mCamera.autoFocus(autoFocusCallback);
                    }
                }, 10);// 每10毫秒连续聚焦
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();

        // Open the default i.e. the first rear facing camera.
        mCamera = Camera.open();
        cameraCurrentlyLocked = defaultCameraId;
        mPreview.setCamera(mCamera);
    }

    @Override
    protected void onPause() {
        super.onPause();

        // Because the Camera object is a shared resource, it's very
        // important to release it when the activity is paused.
        if (mCamera != null) {
            mPreview.setCamera(null);
            mCamera.release();
            mCamera = null;
        }
    }

    public void prepareCameraLayout() {
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) previewView.getLayoutParams();
        layoutParams.width = cameraWidth;
        layoutParams.height = cameraHeight;
        previewView.setLayoutParams(layoutParams);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate our menu which can gather user input for switching camera
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.camera_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.switch_cam:
                // check for availability of multiple cameras
                if (numberOfCameras == 1) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setMessage("Device has only one camera!")
                        .setNeutralButton("Close", null);
                    AlertDialog alert = builder.create();
                    alert.show();
                    return true;
                }

                // OK, we have multiple cameras.
                // Release this camera -> cameraCurrentlyLocked
                if (mCamera != null) {
                    mCamera.stopPreview();
                    mPreview.setCamera(null);
                    mCamera.release();
                    mCamera = null;
                }

                // Acquire the next camera and request Preview to reconfigure
                // parameters.
                mCamera = Camera
                    .open((cameraCurrentlyLocked + 1) % numberOfCameras);
                cameraCurrentlyLocked = (cameraCurrentlyLocked + 1)
                    % numberOfCameras;
                mPreview.switchCamera(mCamera);

                // Start the preview
                mCamera.startPreview();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int cardWidth = width * 8 / 10;
        int cardHeight = (int) (cardWidth / 1.58f);
        //        canvas.drawRect((width - cardWidth) / 2 + cardWidth*4/13f, (height - cardHeight) / 2 + cardHeight * 10f/ 13 , (width - cardWidth) / 2 + cardWidth*12/13f,  (height - cardHeight) / 2 + cardHeight*12/13 , paint);
        //        Bitmap noBitmap = bitmap.createBitmap(bitmap, (float)50 + cardWidth * 4f/13, (height - cardHeight) / 2f + cardHeight * 10f / 13, cardWidth * 8f / 13, cardHeight * 2f / 13);
        Bitmap noBitmap = bitmap.createBitmap(bitmap, (int) ((width - cardWidth) / 2 + cardWidth * 4f / 13), (int) ((height - cardHeight) / 2f + cardHeight * 10f / 13), (int) (cardWidth * 8f / 13), (int) (cardHeight * 2f / 13));
        resultImageView.setVisibility(View.VISIBLE);
        previewView.setVisibility(View.GONE);
        resultImageView.setImageBitmap(noBitmap);
    }

    @Override
    public void onBackPressed() {

        if (previewView.getVisibility() == View.GONE && resultImageView.getVisibility() == View.VISIBLE) {
            previewView.setVisibility(View.VISIBLE);
            resultImageView.setVisibility(View.GONE);
            //restart camera
            // OK, we have multiple cameras.
            // Release this camera -> cameraCurrentlyLocked
            if (mCamera != null) {
                mCamera.startPreview();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mCamera.autoFocus(autoFocusCallback);
                    }
                }, 1000);// 每10毫秒连续聚焦
            }
        } else {
            super.onBackPressed();
        }
    }
}

// ----------------------------------------------------------------------

/**
 * A simple wrapper around a Camera and a SurfaceView that renders a centered preview of the Camera
 * to the surface. We need to center the SurfaceView because not all devices have cameras that
 * support preview sizes at the same aspect ratio as the device's display.
 */
class Preview extends ViewGroup implements SurfaceHolder.Callback {
    private final String TAG = "Preview";

    SurfaceView mSurfaceView;
    SurfaceHolder mHolder;
    Camera.Size mPreviewSize;
    List<Camera.Size> mSupportedPreviewSizes;
    Camera mCamera;
    private CameraPreview activity;

    public Handler handler;

    Preview(Context context) {
        super(context);
        activity = (CameraPreview) context;
        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void setCamera(Camera camera) {
        mCamera = camera;
        if (mCamera != null) {
            mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
            requestLayout();
        }
    }

    public void switchCamera(Camera camera) {
        setCamera(camera);
        try {
            camera.setPreviewDisplay(mHolder);
        } catch (IOException exception) {
            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
        Camera.Parameters parameters = camera.getParameters();
        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        requestLayout();

        camera.setParameters(parameters);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // We purposely disregard child measurements because act as a
        // wrapper to a SurfaceView that centers the camera preview instead
        // of stretching it.
        final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(width, height);
        Log.v(TAG, "preview width:" + width + ", height:" + height);
        activity.previewWidth = width;
        activity.previewHeight = height;

        if (mSupportedPreviewSizes != null) {
            mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
        }
        Log.v(TAG, "camera width:" + mPreviewSize.width + ", height:" + mPreviewSize.height);
        activity.cameraWidth = mPreviewSize.width;
        activity.cameraHeight = mPreviewSize.height;
        handler.sendEmptyMessageDelayed(0, 1000);
        //        activity.prepareCameraLayout();
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        if (changed && getChildCount() > 0) {
            final View child = getChildAt(0);

            final int width = r - l;
            final int height = b - t;

            int previewWidth = width;
            int previewHeight = height;
            if (mPreviewSize != null) {
                previewWidth = mPreviewSize.width;
                previewHeight = mPreviewSize.height;
            }

            // Center the child SurfaceView within the parent.
            if (width * previewHeight > height * previewWidth) {
                final int scaledChildWidth = previewWidth * height / previewHeight;
                child.layout((width - scaledChildWidth) / 2, 0,
                    (width + scaledChildWidth) / 2, height);
            } else {
                final int scaledChildHeight = previewHeight * width / previewWidth;
                child.layout(0, (height - scaledChildHeight) / 2,
                    width, (height + scaledChildHeight) / 2);
            }
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try {
            if (mCamera != null) {
                mCamera.setPreviewDisplay(holder);
            }
        } catch (IOException exception) {
            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (mCamera != null) {
            mCamera.stopPreview();
        }
    }


    private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
        final double ASPECT_TOLERANCE = 0.1;
        double targetRatio = (double) w / h;
        if (sizes == null) {
            return null;
        }

        Camera.Size optimalSize = null;
        double minDiff = Double.MAX_VALUE;

        int targetHeight = h;

        // Try to find an size match aspect ratio and size
        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) {
                continue;
            }
            if (Math.abs(size.height - targetHeight) < minDiff) {
                optimalSize = size;
                minDiff = Math.abs(size.height - targetHeight);
            }
        }

        // Cannot find the one match the aspect ratio, ignore the requirement
        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
        parameters.setPictureSize(mPreviewSize.width, mPreviewSize.height);
        requestLayout();

        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}

class CardHintView extends View {

    private Paint paint = new Paint();

    public CardHintView(Context context) {
        super(context);

    }

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

    }

    public CardHintView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        //        canvas.drawColor(0X60000000);
        //
        paint.setStyle(Paint.Style.STROKE);
        int cardWidth = width * 8 / 10;
        int cardHeight = (int) (cardWidth / 1.58f);
        paint.setColor(0XFF00ccff);
        paint.setStrokeWidth(4);
        canvas.drawRect((width - cardWidth) / 2, (height - cardHeight) / 2, width - (width - cardWidth) / 2, height - (height - cardHeight) / 2, paint);

        paint.setColor(0XFFFF0000);
        canvas.drawRect((width - cardWidth) / 2 + cardWidth * 4 / 13f, (height - cardHeight) / 2 + cardHeight * 10f / 13, (width - cardWidth) / 2 + cardWidth * 12 / 13f, (height - cardHeight) / 2 + cardHeight * 12 / 13, paint);
        invalidate();
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics