android 拍照

方法1.调用api拍照并保存
拍照takepicture
package com.example.takepicture;

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

import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	
	private Button mButtonFocus;
	private Button mButtonTake;
	private View mLayout;
	private SurfaceView mSurfaceView;
	private Camera camera;
	
	private final static String TAG="takepicture";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
		Window window =getWindow();
	    requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
	    window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏


	    
	    setContentView(R.layout.activity_main);
	    
	    
	    mButtonFocus =(Button) findViewById(R.id.buttonfocus);
	    mButtonTake =(Button) findViewById(R.id.buttontake);
	    mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
	    
	    
	    mSurfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	    mSurfaceView.getHolder().setFixedSize(176, 144);
	    mSurfaceView.getHolder().setKeepScreenOn(true);
	    
	    mSurfaceView.getHolder().addCallback(new SurfaceCallBack());
	    
	    
	    
	    mButtonFocus.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				camera.autoFocus(null);
				
				
			}
		});
	    

	    
	    
	    
	    mButtonTake.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				camera.takePicture(null, null, new MyPictureCallBack());
			}
		});
	    
	    
	    
	}
	
	
    
   private  final class MyPictureCallBack implements PictureCallback
    {

		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			// TODO Auto-generated method stub
			
			
			try {
				
				File jpgFile = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".jpg");
				
				Log.e(TAG, jpgFile.getName());
				FileOutputStream outStream = new FileOutputStream(jpgFile);
				
				outStream.write(data);
				outStream.close();
				
				camera.startPreview();
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
    	
    	
    }
	
	private final class SurfaceCallBack implements Callback {

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			
			try {
				
			camera =Camera.open();
			Camera.Parameters param = camera.getParameters();
			Log.i(TAG, param.flatten());
			
			param.setPreviewSize(800, 480);
			param.setPreviewFrameRate(5);
			param.setPictureSize(1024, 768);
			param.setJpegQuality(80);
			camera.setParameters(param);
			camera.setPreviewDisplay(holder);
			camera.startPreview();	
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

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

		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub

			if(camera != null)
				camera.release();
		}

	}
	
	

	@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;
	}






	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		if(event.getAction()==MotionEvent.ACTION_DOWN)
		{
			mLayout =(View)	findViewById(R.id.buttonlayout);
			mLayout.setVisibility(ViewGroup.VISIBLE);
			
			return true;
		}
		
		
		
		return super.onTouchEvent(event);
	}

}




方法2 调用activity拍照并保存
CameraIntentDemo

你可能感兴趣的:(android 拍照)