使用摄像头拍照

时间:2022-11-17 10:24:11

在main.xml布局文件添加用于显示取景画面的SurfaceView 控件:

<SurfaceView android:layout_width="fill_parent" android:layout_height="240dip" android:id="@+id/surfaceView" />

SurfaceView surfaceView = (SurfaceView)this.findViewById(R.id.surfaceView);

surfaceView.getHolder().setFixedSize(176, 144);  //设置分辨率

/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/

surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Camera camera = Camera.open();

WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

Display display = wm.getDefaultDisplay();

Camera.Parameters parameters = camera.getParameters();

parameters.setPreviewSize(display.getWidth(), display.getHeight());//设置预览照片的大小

parameters.setPreviewFrameRate(3);//每秒3帧

parameters.setPictureFormat(PixelFormat.JPEG);//设置照片的输出格式

parameters.set("jpeg-quality", 85);//照片质量

parameters.setPictureSize(display.getWidth(), display.getHeight());//设置照片的大小

camera.setParameters(parameters);

camera.setPreviewDisplay(surfaceView.getHolder());//通过SurfaceView显示取景画面

camera.startPreview();//开始预览

camera.autoFocus(null);//自动对焦

camera.takePicture(null, null, null, jpegCallback);//拍照片

camera.stopPreview();//停止预览

camera.release();//释放摄像头

使用摄像头拍照使用摄像头拍照AndroidManifest.xml
   
   
   
<? xml version="1.0" encoding="utf-8" ?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package
="com.takepic"
android:versionCode
="1"
android:versionName
="1.0" >
< uses-sdk android:minSdkVersion ="7" />

< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
<!-- 手机屏幕横向显示 -->
< activity android:name =".TakePicActivity"
android:label
="@string/app_name" android:screenOrientation ="landscape" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >

</ application >
<!-- 增加摄像头的访问权限 -->
< uses-permission android:name ="android.permission.CAMERA" />
<!-- 在SDCard中创建与删除文件权限 -->
< uses-permission android:name ="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
< uses-permission android:name ="android.permission.WRITE_EXTERNAL_STORAGE" />
</ manifest >
使用摄像头拍照使用摄像头拍照main.xml
   
   
   
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< SurfaceView
android:layout_width ="fill_parent"
android:layout_height
="fill_parent"
android:id
="@+id/surfaceview"
/>
</ LinearLayout >
使用摄像头拍照使用摄像头拍照TakePicActivity
   
   
   
package com.takepic;

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

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;

public class TakePicActivity extends Activity {
private final static String TAG = " TakePicActivity " ;
private SurfaceView surfaceView;
private Camera camera;
private boolean preview = false ;
/** Called when the activity is first created. */
@Override
public 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); // 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮
setContentView(R.layout.main);
surfaceView
= (SurfaceView) this .findViewById(R.id.surfaceview);
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(
176 , 144 ); // 设置分辨率

}

@Override
public boolean onKeyDown( int keyCode, KeyEvent event) {
if (camera != null && event.getRepeatCount() == 0 ){ // 按单次
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH:
camera.autoFocus(
null ); // 自动对焦
break ;
case KeyEvent.KEYCODE_CAMERA: // 拍照键
case KeyEvent.KEYCODE_DPAD_CENTER: // 确认键
camera.takePicture( null , null , new TackPicCallBack());
break ;

default :
break ;
}
}
return true ;
}

private final class TackPicCallBack implements PictureCallback{

@Override
public void onPictureTaken( byte [] data, Camera camera) {
FileOutputStream stream
= null ;
try {
Bitmap bitmap
= BitmapFactory.decodeByteArray(data, 0 , data.length);
File file
= new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + " .jpg " );
stream
= new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG,
100 , stream);
// 处理完照片继续预览
camera.stopPreview();
camera.startPreview();
}
catch (Exception e) {
Log.i(TAG, e.getMessage());
}
finally {
if (stream != null ){
try {
stream.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

private final class SurfaceViewCallBack 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();
Camera.Parameters parameters
= camera.getParameters();
WindowManager wm
= (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display
= wm.getDefaultDisplay();
parameters.setPreviewSize(display.getWidth(), display.getHeight());
// 设置预览照片的大小
parameters.setPreviewFrameRate( 3 ); // 每秒3帧
parameters.setPictureFormat(PixelFormat.JPEG); // 设置照片的输出格式
parameters.set( " jpeg-quality " , 85 ); // 照片质量 最高质量可以设置为100
parameters.setPictureSize(display.getWidth(), display.getHeight()); // 设置照片的大小 如果要拍的大就设置大一些
camera.setParameters(parameters);
camera.setPreviewDisplay(surfaceView.getHolder());
// 通过SurfaceView显示取景画面
camera.startPreview(); // 开始预览
preview = true ;
}
catch (Exception e) {
Log.i(TAG, e.getMessage());
}
// preview = true;

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera != null ){
if (preview){
camera.stopPreview();
// 停止预览
}
camera.release();
}

}

}
}