- 浏览: 1225016 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (353)
- java基础 (40)
- tapestry (16)
- gwt (14)
- ajax (7)
- linux (8)
- ubuntu (18)
- eclipse (6)
- dojo (2)
- javascript (7)
- maven (4)
- 日常生活 (29)
- web2.0 (2)
- jsonrpc (1)
- compass (1)
- lucene (0)
- spring (10)
- cluster (3)
- 数据结构 (2)
- sqlserver (1)
- tomcat (2)
- swing (1)
- servlet (1)
- hibernate (1)
- firefox (1)
- Java-Puzzlers (1)
- Wicket (2)
- mysql (10)
- windows (5)
- Webwork (1)
- struts2 (1)
- Seam (2)
- jboss (1)
- idea (6)
- 分布计算 (3)
- Python-Django (7)
- Hadoop (1)
- 工具 (3)
- Tokyo Tyrant (2)
- Comet (1)
- android (115)
- 音乐 (6)
- cxf (1)
- mqtt (1)
最新评论
-
hesai_vip:
学习了,感谢
gradlew wrapper使用下载到本地的gradle.zip文件安装。 -
imknown:
姚瑶大坏蛋 写道我也遇到了这个,用你这个方法,导致下拉刷新不起 ...
解决android-Ultra-Pull-To-Refresh下拉刷新组件中嵌套ViewPager的一个bug -
姚瑶大坏蛋:
我也遇到了这个,用你这个方法,导致下拉刷新不起作用了,你遇到过 ...
解决android-Ultra-Pull-To-Refresh下拉刷新组件中嵌套ViewPager的一个bug -
寐语者:
LeaderElection(String zookeeper ...
使用Zookeeper来为你的程序加上Leader Election的功能。 -
fyc0109:
博主, 安装了一部分还是报这个错误!FAILURE: Buil ...
gradlew wrapper使用下载到本地的gradle.zip文件安装。
主要是利用Camera的拍照,画了几个框,然后Bitmap的裁剪。自动对焦成功5次之后自动拍照,其中处理照片放在了UI线程, 大家做的时候可以放到后端处理更好。
大家可以看看官方文档tranning和api guide里面关于Camera这块的文章,还有apiDemos里面的CameraPreview.java。看完基本上就能做一个独立的摄像头app了。
大家可以看看官方文档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(); } }
- myapp.zip (405.3 KB)
- 下载次数: 45
发表评论
-
安卓各种长知识的技术文章,不断更新
2015-10-27 18:24 796http://blog.udinic.com/2015/09 ... -
发现一个android sdk更新神速的镜像
2015-09-02 11:26 952mirrors.dormforce.net:80 //(栋力无 ... -
gradle项目中怎么集中管理你的依赖库
2015-08-16 11:30 736项目结构 引用root --gradleScript ... -
使用Dexposed为你的安卓应用加上在线修复的能力。
2015-08-11 16:15 6142移动客户端应用相对于W ... -
解决android-Ultra-Pull-To-Refresh下拉刷新组件中嵌套ViewPager的一个bug
2015-08-02 00:15 17076在项目中使用了android-Ultra-Pull-To-Re ... -
android有质量的面试题
2015-03-17 14:31 956https://github.com/android-cn/i ... -
提高Android Studio的gradle编译速度。
2015-03-12 14:12 4418发现用Android Studio的make和直接使用comm ... -
gradlew wrapper使用下载到本地的gradle.zip文件安装。
2015-03-12 01:26 15479使用gradlew来build项目时,有时候老是需要下载gra ... -
gradlew wrapper设置代理下载gradle
2015-02-27 18:25 45981. HTTP Only Proxy configuratio ... -
认识mqtt
2015-01-30 11:43 1109http://www.hivemq.com/mqtt-esse ... -
Google的Android性能模式
2015-01-14 14:44 775http://www.infoq.com/cn/news/20 ... -
反对Fragment的理由,顺便叫你如果创建自己的自定义view
2014-12-18 17:08 1131下面这篇文章讲了反对Fragment的理由,顺便叫你如果创建自 ... -
获取status bar的高度
2014-12-16 19:52 1316public static int getStatus ... -
最新的appCompat support library中,actionbar的自定义view不能占用全部的宽度
2014-12-02 23:22 956<style name="AppTheme&q ... -
ScrollTricks实现StickHeader效果
2014-11-06 19:07 1139https://code.google.com/p/roman ... -
AppCompat v21 — Material Design for Pre-Lollipop Devices!
2014-10-23 11:03 898http://android-developers.blogs ... -
国内Android Sdk Manager不能更新的问题。
2014-09-15 15:49 982加hosts 引用203.208.46.146 dl.goo ... -
在Centos上安装android sdk遇到的问题。
2014-09-15 12:00 1399安装好android sdk后, 打包时老是会有下面的问题。 ... -
Android的一些制作图表按钮的工具
2014-09-09 16:36 1554Android Button Maker Android P ... -
DESIGNER'S GUIDE TO DPI
2014-08-29 14:37 482强烈建议美工看看, 对于移动app的多密度屏幕的支持 http ...
相关推荐
综上所述,创建一个Android自定义相机身份证扫描界面涉及多个步骤,包括权限管理、相机API的使用、自定义布局设计、手势识别、图像处理等技术。通过对这些知识点的掌握和实践,你可以为用户提供一个高效且准确的...
身份证号码的实时扫描通常需要结合Android的Camera API或CameraX库。通过开启摄像头预览,捕获每一帧图像,然后传递给OCR引擎进行识别。确保处理速度足够快,以避免用户体验下降。 5. **图像处理**: 在将图片...
1. **图像捕获与预处理**:使用Android的Camera API或其他图像库获取身份证的图像,然后应用OpenCV进行图像增强,去除噪声,使文字更清晰。 2. **文字区域检测**:通过边缘检测或模板匹配找到身份证上的文字区域。 ...
8. **网络要求**:稳定且速度适当的WiFi连接是使用Android WiFi Camera的关键,网络带宽直接影响视频流的清晰度和流畅性。 9. **硬件兼容性**:并非所有Android设备都支持此功能,因为摄像头质量和硬件加速能力可能...
androidusbcamera是在saki4510t/uvc camera的基础上开发的,usb摄像机(uvc设备)项目和视频数据采集的使用都是高度封装的,它可以通过几个简单的api帮助开发者轻松使用usb摄像机设备。通过使用androidusbcamera,您...
Android Studio提供了强大的集成开发环境,而Camera2 API则是Android系统提供给开发者用于访问和控制摄像头的高级接口。本篇将详细介绍如何利用MediaRecorder和Camera2框架来实现一个基本的录像功能。 1. **Camera2...
在Android平台上,USB Camera(UVC,Universal Video Class)是一种允许设备通过USB接口连接并作为摄像头使用的功能。本文将深入探讨Android USBCamera和UVCCamera的开发,以及如何利用通用库来实现这一功能。 首先...
Camera2Basic是Android官方提供的一个基础示例,用于演示如何使用Camera2 API创建一个简单的拍照应用。 二、项目结构分析 "android-Camera2Basic-master"项目主要包含以下几个部分: 1. `app`:应用模块,包含...
Android 平台支持使用即插即用的 USB 摄像头(即网络摄像头),但前提是这些摄像头采用标准的 Android Camera2 API 和摄像头 HIDL 接口。网络摄像头通常支持 USB 视频类 (UVC) 驱动程序,并且在 Linux 上,系统采用...
本示例将深入探讨如何在Android应用中有效地使用Camera API,以及MediaRecorder类,以实现拍照、录像等功能。 1. **Camera API简介** Android Camera API分为两代:Camera1(旧版)和Camera2(新版)。Camera1 API...
5. **Camera2 API**: 虽然名称是“androidusbcamera”,但考虑到描述中的功能,可能使用了Camera2 API而不是传统的Camera API。Camera2 API提供了更高级别的控制,能更好地支持UVC摄像头的特性,如调整分辨率和色彩...
在Android平台上,Camera2 API是用于访问和控制设备相机功能的高级接口,相较于早期的Camera API,Camera2提供了更精细的控制和更好的性能。本文将深入探讨如何使用Camera2 API来实现预览和拍照功能。 一、Camera2 ...
因为工作中要使用 Android Camera2 API 来实现相机功能,但因为Camera2比较复杂,网上资料也比较乱,有一定入门门槛,所以花了几天时间系统研究了下,这个项目就是我研究的成果。 其中包括一个自己写的Camera2的Demo...
`Camera`类的使用涉及到权限设置,如需访问相机,需要在`AndroidManifest.xml`中声明`<uses-permission android:name="android.permission.CAMERA" />`。 ### 摄像头预览 在Android相机应用中,首先需要创建一个...
在Android系统中,USB Camera(通用串行总线相机)是一种允许设备通过USB接口连接到Android设备并作为摄像头使用的功能。这种技术对于扩展Android设备的摄像头功能,例如在没有内置摄像头或者需要更高性能摄像头的...
在Android开发中,读取身份证信息是一项常见的需求,特别是在金融、交通、医疗等领域。这个名为"基于Android Studio的读取身份证信息源码.zip"的压缩包提供了一个完整的示例项目——IDcardDemo,用于演示如何在...
在Android 4.0(Ice Cream Sandwich)系统中,Camera模块是系统的重要组成部分,它为开发者提供了丰富的功能,包括拍照、录制视频、设置参数等。本文将深入探讨Android 4.0 Camera源码中的关键知识点。 一、Camera...
apk文件,android uvc Camera Demo,支持录像、拍照。
- 不同的Android设备可能对Camera API有不同的支持程度,因此在开发过程中要考虑设备兼容性,可能需要使用Support Library或第三方库。 9. **Camera2 API简介** - 如果需要更高级的控制,如手动对焦、曝光控制等...