`
dengyin2000
  • 浏览: 1225035 次
  • 性别: 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();
    }
}
分享到:
评论

相关推荐

    Android自定义相机——身份证扫描界面

    综上所述,创建一个Android自定义相机身份证扫描界面涉及多个步骤,包括权限管理、相机API的使用、自定义布局设计、手势识别、图像处理等技术。通过对这些知识点的掌握和实践,你可以为用户提供一个高效且准确的...

    Android Ocr文字识别 实现身份证号码实时扫描

    身份证号码的实时扫描通常需要结合Android的Camera API或CameraX库。通过开启摄像头预览,捕获每一帧图像,然后传递给OCR引擎进行识别。确保处理速度足够快,以避免用户体验下降。 5. **图像处理**: 在将图片...

    **基于opencv库和tess-two在android平台上实现身份证号的识别!**

    1. **图像捕获与预处理**:使用Android的Camera API或其他图像库获取身份证的图像,然后应用OpenCV进行图像增强,去除噪声,使文字更清晰。 2. **文字区域检测**:通过边缘检测或模板匹配找到身份证上的文字区域。 ...

    Android WiFi Camera (无线局域网IP摄像头)

    8. **网络要求**:稳定且速度适当的WiFi连接是使用Android WiFi Camera的关键,网络带宽直接影响视频流的清晰度和流畅性。 9. **硬件兼容性**:并非所有Android设备都支持此功能,因为摄像头质量和硬件加速能力可能...

    AndroidUSBCamera,通用uvccamera库,支持录制、推送等功能.zip

    androidusbcamera是在saki4510t/uvc camera的基础上开发的,usb摄像机(uvc设备)项目和视频数据采集的使用都是高度封装的,它可以通过几个简单的api帮助开发者轻松使用usb摄像机设备。通过使用androidusbcamera,您...

    Android Studio Camera2的录像功能

    Android Studio提供了强大的集成开发环境,而Camera2 API则是Android系统提供给开发者用于访问和控制摄像头的高级接口。本篇将详细介绍如何利用MediaRecorder和Camera2框架来实现一个基本的录像功能。 1. **Camera2...

    Android USBCamera,UVCCamera开发通用库

    在Android平台上,USB Camera(UVC,Universal Video Class)是一种允许设备通过USB接口连接并作为摄像头使用的功能。本文将深入探讨Android USBCamera和UVCCamera的开发,以及如何利用通用库来实现这一功能。 首先...

    android-Camera2Basic-master源码

    Camera2Basic是Android官方提供的一个基础示例,用于演示如何使用Camera2 API创建一个简单的拍照应用。 二、项目结构分析 "android-Camera2Basic-master"项目主要包含以下几个部分: 1. `app`:应用模块,包含...

    Android通过Camera API接口控制外接 USB 摄像头.pdf

    Android 平台支持使用即插即用的 USB 摄像头(即网络摄像头),但前提是这些摄像头采用标准的 Android Camera2 API 和摄像头 HIDL 接口。网络摄像头通常支持 USB 视频类 (UVC) 驱动程序,并且在 Linux 上,系统采用...

    Android中Camera使用案例

    本示例将深入探讨如何在Android应用中有效地使用Camera API,以及MediaRecorder类,以实现拍照、录像等功能。 1. **Camera API简介** Android Camera API分为两代:Camera1(旧版)和Camera2(新版)。Camera1 API...

    androidusbcamera.rar

    5. **Camera2 API**: 虽然名称是“androidusbcamera”,但考虑到描述中的功能,可能使用了Camera2 API而不是传统的Camera API。Camera2 API提供了更高级别的控制,能更好地支持UVC摄像头的特性,如调整分辨率和色彩...

    Android原生Camera2完成预览和拍照

    在Android平台上,Camera2 API是用于访问和控制设备相机功能的高级接口,相较于早期的Camera API,Camera2提供了更精细的控制和更好的性能。本文将深入探讨如何使用Camera2 API来实现预览和拍照功能。 一、Camera2 ...

    Android Camera2 Demo - 实现相机预览、拍照、录制视频功能

    因为工作中要使用 Android Camera2 API 来实现相机功能,但因为Camera2比较复杂,网上资料也比较乱,有一定入门门槛,所以花了几天时间系统研究了下,这个项目就是我研究的成果。 其中包括一个自己写的Camera2的Demo...

    android-Camera-Code.zip_android 相机_android 相机_camera_camera and

    `Camera`类的使用涉及到权限设置,如需访问相机,需要在`AndroidManifest.xml`中声明`&lt;uses-permission android:name="android.permission.CAMERA" /&gt;`。 ### 摄像头预览 在Android相机应用中,首先需要创建一个...

    Android系统 USBCamera使用案例

    在Android系统中,USB Camera(通用串行总线相机)是一种允许设备通过USB接口连接到Android设备并作为摄像头使用的功能。这种技术对于扩展Android设备的摄像头功能,例如在没有内置摄像头或者需要更高性能摄像头的...

    基于Android Stuido的读取身份证信息源码.zip

    在Android开发中,读取身份证信息是一项常见的需求,特别是在金融、交通、医疗等领域。这个名为"基于Android Studio的读取身份证信息源码.zip"的压缩包提供了一个完整的示例项目——IDcardDemo,用于演示如何在...

    android4.0camera源码

    在Android 4.0(Ice Cream Sandwich)系统中,Camera模块是系统的重要组成部分,它为开发者提供了丰富的功能,包括拍照、录制视频、设置参数等。本文将深入探讨Android 4.0 Camera源码中的关键知识点。 一、Camera...

    android uvc Camera Demo

    apk文件,android uvc Camera Demo,支持录像、拍照。

    android自定义Camera实现

    - 不同的Android设备可能对Camera API有不同的支持程度,因此在开发过程中要考虑设备兼容性,可能需要使用Support Library或第三方库。 9. **Camera2 API简介** - 如果需要更高级的控制,如手动对焦、曝光控制等...

Global site tag (gtag.js) - Google Analytics