Android中拍照简单实现方法2

  categories:android资料  author:

原创文章,转载请指明出处并保留原文url地址

Android中拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据。 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,这里是采用第二中方法实现的一个拍照演示程序

我们采用第一种方法, 创建一个演示程序,显示界面如下:

wps_clip_image-29819_thumb[1]

预览照片

wps_clip_image-22011_thumb[1]

点击屏幕后, 显示对焦及拍照按钮

wps_clip_image-10874_thumb[1]

点击 , 对焦按钮进行对焦, 点击拍照按钮, 进行拍照

创建新的项目

1.在Eclipse IDE中创建一个新的项目并把填入必须的信息。 File->New->Android Project

创建完项目结构如下:

wps_clip_image-3503_thumb[1]

AndroidManifest.xml 文件

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="cn.iigrowing.www.android.take.carema"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk android:minSdkVersion="9" />

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

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

<application

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

<activity

android:label="@string/app_name"

android:name="cn.iigrowing.www.android.take.carema.TakePicActivity"

android:screenOrientation="landscape" >

<intent-filter >

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

TakePicActivity.java

/*

* Copyright (C) 2013 carema authors

* @author shao

* @date 2013.9.26

* @blog http://www.iigrowing.cn/

* 本程序是一个android客户端软件演示程序

*

*/

package cn.iigrowing.www.android.take.carema;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import android.app.Activity;

import android.hardware.Camera;

import android.hardware.Camera.PictureCallback;

import android.os.Bundle;

import android.os.Environment;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

import android.view.View;

import android.view.Window;

import android.view.WindowManager;

public class TakePicActivity extends Activity {

private View layout;

private Camera camera;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// 下面这两个设置可以在配置文件中实现, 这里使用程序中实现的方法

// 设置没有标题

requestWindowFeature(Window.FEATURE_NO_TITLE);

// 设置全屏  

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.main);

layout = this.findViewById(R.id.buttonLayout);

SurfaceView sView = (SurfaceView) findViewById(R.id.surPic);

//摄像头画面直接输出过来

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

sView.getHolder().setFixedSize(176, 144);

// 保持屏幕高亮, 不要锁定

sView.getHolder().setKeepScreenOn(true);

sView.getHolder().addCallback(new SurfaceCallback());

}

public void takepicture(View v) {

if (camera != null) {

switch (v.getId()) {

case R.id.takepicture:

camera.takePicture(null, null, new MyPictureCallback());

break;

case R.id.autofouse:

camera.autoFocus(null);

break;

}

}

}

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");

FileOutputStream outputStream = new FileOutputStream(jpgFile);

outputStream.write(data);

outputStream.close();

camera.startPreview();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private final class SurfaceCallback implements SurfaceHolder.Callback {

public void surfaceCreated(SurfaceHolder houlder) {

try {

// 开始预览

camera = Camera.open();

Camera.Parameters parameters = camera.getParameters();

parameters.setPreviewSize(800, 480);

parameters.setPreviewFrameRate(5);

parameters.setPictureSize(1024, 768);

parameters.setJpegQuality(80);

camera.setParameters(parameters);

camera.setPreviewDisplay(houlder);

camera.startPreview();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void surfaceDestroyed(SurfaceHolder holder) {

if (camera != null) {

// 释放照相机, 只有释放了, 其他程序才能使用

camera.release();

camera = null;

}

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

// TODO Auto-generated method stub

}

}

@Override

public boolean onTouchEvent(MotionEvent event) {

// 显示隐藏的按钮等

if (event.getAction() == MotionEvent.ACTION_DOWN) {

layout.setVisibility(View.VISIBLE);

return true;

}

return super.onTouchEvent(event);

}

}

源代码 下载地址



快乐成长 每天进步一点点