手机拍照功能的简单实现

1、main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    

    <SurfaceView 
        android:id="@+id/surfaceview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />
    
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:orientation="horizontal"
        >
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="对焦"
            android:onClick="focus"
            android:layout_marginRight="10dp"
            />
        
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拍照"
            android:onClick="takepicture"
            android:layout_marginRight="10dp"
            />
    </LinearLayout>
</RelativeLayout>


2、MainActivity

package com.njupt.takepicture1;

import java.io.File;
import java.io.FileOutputStream;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

	private SurfaceView surfaceview;
	private Camera camera;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
		
		surfaceview = (SurfaceView) findViewById(R.id.surfaceview);
		SurfaceHolder holder = surfaceview.getHolder();
		holder.setFixedSize(176, 144);
		holder.setKeepScreenOn(true);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		holder.addCallback(new MySurfaceCallback());
	}

	private class MySurfaceCallback implements Callback {
		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			// TODO Auto-generated method stub

		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			try {
				camera = Camera.open();
				Parameters params = camera.getParameters();
				params.setJpegQuality(90);
				params.setPictureSize(1024, 768);
				params.setPreviewFrameRate(10);

				camera.setParameters(params);
				camera.setPreviewDisplay(surfaceview.getHolder());
				camera.startPreview();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			if (camera != null) {
				camera.release();
				camera = null;
			}
		}
	}

	public void takepicture(View v) {
		camera.takePicture(null, null, new MyPictureCallback());
	}

	private class MyPictureCallback implements PictureCallback {
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			try {
				File file = new File(Environment.getExternalStorageDirectory(),
						System.currentTimeMillis() + ".jpg");
				FileOutputStream fos = new FileOutputStream(file);
				fos.write(data);
				camera.startPreview();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


3、AndroidManifest.xml

注册权限

<uses-permission android:name="android.permission.CAMERA"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>


横屏显示:

<activity
            android:name="com.njupt.takepicture1.MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation="landscape">


你可能感兴趣的:(手机拍照功能的简单实现)