这个代码几乎涉及到了摄像头开发的所有方面,(除了PreviewCallback,这块东西我会结合android摄像头自动识别人脸/火灾来谈),且力求精简,是杂家的心血阿!相对之前改进之处有:
1,精简。只有一个ImageButton用来实现按下拍照。拍照后自动保存,进入预览界面。 不像原来的要三个按键:预览/拍照/保存。
2,聚焦方面实现不间断循环聚焦。 不像之前的,要按一下按键聚焦一次。
3,ImageButton增加了按下的效果。按之前示例如下:,点击后背景变暗,有种风车旋转的感觉。
4,增加了查询摄像头PictureSizes和PreviewSize的代码,调试程序时应先查询出自己的参数然后配置。不同的手机参数不同。另外,预览surfaceView的高我设为800px,如果手机屏幕太小,这个参数要改。
5,改进了之前的按back返回按键退出程序后,再次进入程序camera没有释放,致使程序挂掉的问题。
6,改进了预览时手机横竖屏切换时,程序挂掉的毛病。但这里的布局还是采用默认的竖屏。
7,在实现循环聚焦的同时,保留了autoFocus()接口。可以测试出,在使用FOCUS_MODE_CONTINUOUS_VIDEO聚焦模式下,autoFocus不发挥作用。如果不支持不间断聚焦,setFocusMode就改成FOCUS_MODE_AUTO!!!
8,注释更加良好。
废话不说了请看源码:
第一部分:Manifinest.xml
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="yan.guoqi.rectphoto"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
-
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-feature android:name="android.hardware.camera" />
-
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".RectPhoto"
- android:label="@string/title_activity_rect_photo" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
-
- </manifest>
第二部分:布局文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/BestWish"
- tools:context=".RectPhoto" />
-
- <FrameLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
-
- <SurfaceView
- android:id="@+id/previewSV"
- android:layout_width="fill_parent"
- android:layout_height="800px" />
- </FrameLayout>
-
- <ImageButton
- android:id="@+id/photoImgBtn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/photo_img_btn"
- android:layout_gravity="center" />
-
- </LinearLayout>
第三部分:RectPhoto.java主程序
- package yan.guoqi.rectphoto;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.ColorMatrixColorFilter;
- import android.graphics.Matrix;
- import android.graphics.PixelFormat;
- import android.hardware.Camera;
- import android.hardware.Camera.AutoFocusCallback;
- import android.hardware.Camera.PictureCallback;
- import android.hardware.Camera.ShutterCallback;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.View.OnTouchListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.ImageButton;
-
- public class RectPhoto extends Activity implements SurfaceHolder.Callback{
- private static final String tag="yan";
- private boolean isPreview = false;
- private SurfaceView mPreviewSV = null;
- private SurfaceHolder mySurfaceHolder = null;
- private ImageButton mPhotoImgBtn = null;
- private Camera myCamera = null;
- private Bitmap mBitmap = null;
- private AutoFocusCallback myAutoFocusCallback = null;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
- Window myWindow = this.getWindow();
- myWindow.setFlags(flag, flag);
-
- setContentView(R.layout.activity_rect_photo);
-
-
- mPreviewSV = (SurfaceView)findViewById(R.id.previewSV);
- mySurfaceHolder = mPreviewSV.getHolder();
- mySurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
- mySurfaceHolder.addCallback(this);
- mySurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
-
-
- myAutoFocusCallback = new AutoFocusCallback() {
-
- public void onAutoFocus(boolean success, Camera camera) {
-
- if(success)
- {
- Log.i(tag, "myAutoFocusCallback: success...");
-
-
- }
- else
- {
-
- Log.i(tag, "myAutoFocusCallback: 失败了...");
-
- }
-
-
- }
- };
-
- mPhotoImgBtn = (ImageButton)findViewById(R.id.photoImgBtn);
-
- LayoutParams lp = mPhotoImgBtn.getLayoutParams();
- lp.width = 120;
- lp.height = 120;
- mPhotoImgBtn.setLayoutParams(lp);
- mPhotoImgBtn.setOnClickListener(new PhotoOnClickListener());
- mPhotoImgBtn.setOnTouchListener(new MyOnTouchListener());
-
-
- }
-
-
-
- public void surfaceChanged(SurfaceHolder holder, int format, int width,int height)
-
- {
-
- Log.i(tag, "SurfaceHolder.Callback:surfaceChanged!");
- initCamera();
-
- }
-
-
- public void surfaceCreated(SurfaceHolder holder)
-
- {
-
- myCamera = Camera.open();
- try {
- myCamera.setPreviewDisplay(mySurfaceHolder);
- Log.i(tag, "SurfaceHolder.Callback: surfaceCreated!");
- } catch (IOException e) {
-
- if(null != myCamera){
- myCamera.release();
- myCamera = null;
- }
- e.printStackTrace();
- }
-
-
-
- }
-
-
- public void surfaceDestroyed(SurfaceHolder holder)
-
- {
-
- Log.i(tag, "SurfaceHolder.Callback:Surface Destroyed");
- if(null != myCamera)
- {
- myCamera.setPreviewCallback(null);
-
-
- myCamera.stopPreview();
- isPreview = false;
- myCamera.release();
- myCamera = null;
- }
-
- }
-
-
- public void initCamera(){
- if(isPreview){
- myCamera.stopPreview();
- }
- if(null != myCamera){
- Camera.Parameters myParam = myCamera.getParameters();
-
-
-
-
-
- myParam.setPictureFormat(PixelFormat.JPEG);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- myParam.setPictureSize(1280, 960);
- myParam.setPreviewSize(960, 720);
-
- myCamera.setDisplayOrientation(90);
- myParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
- myCamera.setParameters(myParam);
- myCamera.startPreview();
- myCamera.autoFocus(myAutoFocusCallback);
- isPreview = true;
- }
- }
-
-
- ShutterCallback myShutterCallback = new ShutterCallback()
-
- {
-
- public void onShutter() {
-
- Log.i(tag, "myShutterCallback:onShutter...");
-
- }
- };
- PictureCallback myRawCallback = new PictureCallback()
-
- {
-
- public void onPictureTaken(byte[] data, Camera camera) {
-
- Log.i(tag, "myRawCallback:onPictureTaken...");
-
- }
- };
- PictureCallback myJpegCallback = new PictureCallback()
-
- {
-
- public void onPictureTaken(byte[] data, Camera camera) {
-
- Log.i(tag, "myJpegCallback:onPictureTaken...");
- if(null != data){
- mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
- myCamera.stopPreview();
- isPreview = false;
- }
-
- Matrix matrix = new Matrix();
- matrix.postRotate((float)90.0);
- Bitmap rotaBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, false);
-
- if(null != rotaBitmap)
- {
- saveJpeg(rotaBitmap);
- }
-
-
- myCamera.startPreview();
- isPreview = true;
- }
- };
-
- public class PhotoOnClickListener implements OnClickListener{
-
- public void onClick(View v) {
-
- if(isPreview && myCamera!=null){
- myCamera.takePicture(myShutterCallback, null, myJpegCallback);
- }
-
- }
-
- }
-
- public void saveJpeg(Bitmap bm){
- String savePath = "/mnt/sdcard/rectPhoto/";
- File folder = new File(savePath);
- if(!folder.exists())
- {
- folder.mkdir();
- }
- long dataTake = System.currentTimeMillis();
- String jpegName = savePath + dataTake +".jpg";
- Log.i(tag, "saveJpeg:jpegName--" + jpegName);
-
- try {
- FileOutputStream fout = new FileOutputStream(jpegName);
- BufferedOutputStream bos = new BufferedOutputStream(fout);
-
-
-
-
- bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
- bos.flush();
- bos.close();
- Log.i(tag, "saveJpeg:存储完毕!");
- } catch (IOException e) {
-
- Log.i(tag, "saveJpeg:存储失败!");
- e.printStackTrace();
- }
- }
-
-
- public class MyOnTouchListener implements OnTouchListener{
-
- public final float[] BT_SELECTED=new float[]
- { 2, 0, 0, 0, 2,
- 0, 2, 0, 0, 2,
- 0, 0, 2, 0, 2,
- 0, 0, 0, 1, 0 };
-
- public final float[] BT_NOT_SELECTED=new float[]
- { 1, 0, 0, 0, 0,
- 0, 1, 0, 0, 0,
- 0, 0, 1, 0, 0,
- 0, 0, 0, 1, 0 };
- public boolean onTouch(View v, MotionEvent event) {
-
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED));
- v.setBackgroundDrawable(v.getBackground());
- }
- else if(event.getAction() == MotionEvent.ACTION_UP){
- v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED));
- v.setBackgroundDrawable(v.getBackground());
-
- }
- return false;
- }
-
- }
-
- @Override
- public void onBackPressed()
-
- {
-
- super.onBackPressed();
- RectPhoto.this.finish();
- }
- }
源码下载链接: http://download.csdn.net/detail/yanzi1225627/5060323