package com.jzsoft.fm;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
public class CameraActivity extends Activity implements
CameraPreview.OnCameraStatusListener {
public static final Uri IMAGE_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
public static final String PATH = Environment.getExternalStorageDirectory()
.toString() + "/AndroidMedia/";
private CameraPreview mCameraPreview;
private ImageView focusView;
private boolean isTaking = false; // 拍照中
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置横屏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// 设置全屏
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 设置布局视图
setContentView(R.layout.activity_camera);
// 照相预览界面
mCameraPreview = (CameraPreview) findViewById(R.id.surfaceView);
mCameraPreview.setOnCameraStatusListener(this);
// 焦点图片
focusView = (ImageView) findViewById(R.id.focusView);
focusView.setPadding(mCameraPreview.getWidth() / 2,
mCameraPreview.getHeight() / 2, mCameraPreview.getWidth() / 2,
mCameraPreview.getHeight() / 2);
}
/**
* 触屏事件
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN && !isTaking) {
isTaking = true;
mCameraPreview.takePicture();
}
return super.onTouchEvent(event);
}
/**
* 存储图像并将信息添加入媒体数据库
*/
private Uri insertImage(ContentResolver cr, String name, long dateTaken,
String directory, String filename, Bitmap source, byte[] jpegData) {
OutputStream outputStream = null;
String filePath = directory + filename;
try {
File dir = new File(directory);
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(directory, filename);
if (file.createNewFile()) {
outputStream = new FileOutputStream(file);
if (source != null) {
source.compress(CompressFormat.JPEG, 75, outputStream);
} else {
outputStream.write(jpegData);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (Throwable t) {
}
}
}
ContentValues values = new ContentValues(7);
values.put(MediaStore.Images.Media.TITLE, name);
values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
values.put(MediaStore.Images.Media.DATE_TAKEN, dateTaken);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.Images.Media.DATA, filePath);
return cr.insert(IMAGE_URI, values);
}
/**
* 相机拍照结束事件
*/
@Override
public void onCameraStopped(byte[] data) {
Log.e("onCameraStopped", "==onCameraStopped==");
// 创建图像
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// 系统时间
long dateTaken = System.currentTimeMillis();
// 图像名称
String filename = DateFormat.format("yyyy-MM-dd kk.mm.ss", dateTaken)
.toString() + ".jpg";
// 存储图像(PATH目录)
Uri uri = insertImage(getContentResolver(), filename, dateTaken, PATH,
filename, bitmap, data);
// 返回结果
Intent intent = getIntent();
intent.putExtra("uriStr", uri.toString());
intent.putExtra("dateTaken", dateTaken);
// intent.putExtra("filePath", PATH + filename);
// intent.putExtra("orientation", orientation); // 拍摄方向
setResult(20, intent);
// 关闭当前Activity
finish();
}
/**
* 拍摄时自动对焦事件
*/
@Override
public void onAutoFocus(boolean success) {
// 改变对焦状态图像
if (success) {
focusView.setImageResource(R.drawable.focus2);
} else {
focusView.setImageResource(R.drawable.focus1);
Toast.makeText(this, "焦距不准,请重拍!", Toast.LENGTH_SHORT).show();
isTaking = false;
}
}
}
首先需要继承SurfaceView生成自己的一个类CameraPreview。
package com.jzsoft.fm;
import java.io.IOException;
import android.content.Context;
import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraPreview extends SurfaceView implements
SurfaceHolder.Callback {
/** LOG标识 */
// private static final String TAG = "CameraPreview";
/** 分辨率 */
public static final int WIDTH = 1024;
public static final int HEIGHT = 768;
/** 监听接口 */
private OnCameraStatusListener listener;
private SurfaceHolder holder;
private Camera camera;
// 创建一个PictureCallback对象,并实现其中的onPictureTaken方法
private PictureCallback pictureCallback = new PictureCallback() {
// 该方法用于处理拍摄后的照片数据
@Override
public void onPictureTaken(byte[] data, Camera camera) {
// 停止照片拍摄
camera.stopPreview();
camera = null;
// 调用结束事件
if (null != listener) {
listener.onCameraStopped(data);
}
}
};
// Preview类的构造方法
public CameraPreview(Context context, AttributeSet attrs) {
super(context, attrs);
// 获得SurfaceHolder对象
holder = getHolder();
// 指定用于捕捉拍照事件的SurfaceHolder.Callback对象
holder.addCallback(this);
// 设置SurfaceHolder对象的类型
// holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
// 在surface创建时激发
public void surfaceCreated(SurfaceHolder holder) {
// Log.e(TAG, "==surfaceCreated==");
// 获得Camera对象
camera = Camera.open();
try {
// 设置用于显示拍照摄像的SurfaceHolder对象
camera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
// 释放手机摄像头
camera.release();
camera = null;
}
}
// 在surface销毁时激发
public void surfaceDestroyed(SurfaceHolder holder) {
// Log.e(TAG, "==surfaceDestroyed==");
// 释放手机摄像头
camera.release();
}
// 在surface的大小发生改变时激发
public void surfaceChanged(final SurfaceHolder holder, int format, int w,
int h) {
// Log.e(TAG, "==surfaceChanged==");
try {
// 获取照相机参数
Camera.Parameters parameters = camera.getParameters();
// 设置照片格式
parameters.setPictureFormat(ImageFormat.JPEG);
// 设置预浏尺寸
parameters.setPreviewSize(WIDTH, HEIGHT);
// 设置照片分辨率
parameters.setPictureSize(WIDTH, HEIGHT);
// 设置照相机参数
camera.setDisplayOrientation(90);
camera.setParameters(parameters);
// 开始拍照
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
// 停止拍照,并将拍摄的照片传入PictureCallback接口的onPictureTaken方法
public void takePicture() {
// Log.e(TAG, "==takePicture==");
if (camera != null) {
// 自动对焦
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
if (null != listener) {
listener.onAutoFocus(success);
}
// 自动对焦成功后才拍摄
if (success) {
camera.takePicture(null, null, pictureCallback);
}
}
});
}
}
// 设置监听事件
public void setOnCameraStatusListener(OnCameraStatusListener listener) {
this.listener = listener;
}
/**
* 相机拍照监听接口
*/
public interface OnCameraStatusListener {
// 相机拍照结束事件
void onCameraStopped(byte[] data);
// 拍摄时自动对焦事件
void onAutoFocus(boolean success);
}
}
使用Activity调用