package cn.net.xuefei.myfacedetect; import java.io.IOException; import java.lang.reflect.Method; import android.app.Activity; import android.content.res.Configuration; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.AutoFocusCallback; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; public class MainActivity extends Activity implements SurfaceHolder.Callback { private SurfaceView surfaceview; private SurfaceHolder surfaceholder; private Camera camera = null; Camera.Parameters parameters; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); surfaceview = (SurfaceView) findViewById(R.id.surfaceview); surfaceholder = surfaceview.getHolder(); surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); surfaceholder.addCallback(MainActivity.this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub camera.autoFocus(new AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { if (success) { initCamera();// 实现相机的参数初始化 camera.cancelAutoFocus();// 只有加上了这一句,才会自动对焦。 } } }); } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub // 获取camera对象 camera = Camera.open(); try { // 设置预览监听 camera.setPreviewDisplay(holder); Camera.Parameters parameters = camera.getParameters(); if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { parameters.set("orientation", "portrait"); camera.setDisplayOrientation(90); parameters.setRotation(90); } else { parameters.set("orientation", "landscape"); camera.setDisplayOrientation(0); parameters.setRotation(0); } camera.setParameters(parameters); // 启动摄像头预览 camera.startPreview(); System.out.println("camera.startpreview"); } catch (IOException e) { e.printStackTrace(); camera.release(); System.out.println("camera.release"); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub if (camera != null) { camera.stopPreview(); camera.release(); } } // 相机参数的初始化设置 private void initCamera() { parameters = camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); // parameters.setPictureSize(surfaceView.getWidth(), // surfaceView.getHeight()); // 部分定制手机,无法正常识别该方法。 //parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);// 1连续对焦 setDispaly(parameters, camera); camera.setParameters(parameters); camera.startPreview(); camera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上 } // 控制图像的正确显示方向 private void setDispaly(Camera.Parameters parameters, Camera camera) { if (Integer.parseInt(Build.VERSION.SDK) >= 8) { setDisplayOrientation(camera, 90); } else { parameters.setRotation(90); } } // 实现的图像的正确显示 private void setDisplayOrientation(Camera camera, int i) { Method downPolymorphic; try { downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class }); if (downPolymorphic != null) { downPolymorphic.invoke(camera, new Object[] { i }); } } catch (Exception e) { Log.e("Came_e", "图像出错"); } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <SurfaceView android:id="@+id/surfaceview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.net.xuefei.myfacedetect" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <!-- 摄像头权限 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- SD卡读写权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
参考文章
http://blog.sina.com.cn/s/blog_5a48dd2d0100tmcn.html
http://www.cnblogs.com/over140/archive/2011/11/16/2251344.html
http://blog.csdn.net/lin772662623/article/details/8578548
http://blog.csdn.net/geekstart/article/details/13630009
http://www.cnblogs.com/linlf03/p/3682385.html