Android手机拍照

参考的这个视频教程:http://v.youku.com/v_show/id_XNjI5MzkzMjQ4.html和官方API档file:///D:/Android/androidstudio/sdk/docs/guide/topics/media/camera.html

java程序

 package com.example.x_yp.system_camera;

 import java.io.File;
import java.io.FileOutputStream; import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback; public class MainActivity extends Activity {
protected static final String TAG = "main";
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mCamera = getCameraInstance(); // 创建预览类,并与Camera关联,最后添加到界面布局中
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview); Button captureButton = (Button) findViewById(R.id.button_capture);
captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 在捕获图片前进行自动对焦
mCamera.autoFocus(new AutoFocusCallback() { @Override
public void onAutoFocus(boolean success, Camera camera) {
// 从Camera捕获图片
mCamera.takePicture(null, null, mPicture);
}
});
}
});
} /** 检测设备是否存在Camera硬件 */
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// 存在
return true;
} else {
// 不存在
return false;
}
} /** 打开一个Camera */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
Log.d(TAG, "打开Camera失败失败");
}
return c;
} private PictureCallback mPicture = new PictureCallback() { @Override
public void onPictureTaken(byte[] data, Camera camera) {
// 获取Jpeg图片,并保存在sd卡上
File pictureFile = new File("/sdcard/" + System.currentTimeMillis() + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (Exception e) {
Log.d(TAG, "保存图片失败");
}
}
}; @Override
protected void onDestroy() {
// 回收Camera资源
if(mCamera!=null){
mCamera.stopPreview();
mCamera.release();
mCamera=null;
}
super.onDestroy();
} }
package com.example.x_yp.system_camera;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView; /**
* 定义一个预览类
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "main";
private SurfaceHolder mHolder;
private Camera mCamera; public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera; // 通过SurfaceView获得SurfaceHolder
mHolder = getHolder();
// 为SurfaceHolder指定回调
mHolder.addCallback(this);
// 设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到界面 在Android3.0之后弃用
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} public void surfaceCreated(SurfaceHolder holder) {
// 当Surface被创建之后,开始Camera的预览
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "预览失败");
}
} public void surfaceDestroyed(SurfaceHolder holder) { } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// Surface发生改变的时候将被调用,第一次显示到界面的时候也会被调用
if (mHolder.getSurface() == null){
// 如果Surface为空,不继续操作
return;
} // 停止Camera的预览
try {
mCamera.stopPreview();
} catch (Exception e){
Log.d(TAG, "当Surface改变后,停止预览出错");
} // 在预览前可以指定Camera的各项参数 // 重新开始预览
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview(); } catch (Exception e){
Log.d(TAG, "预览Camera出错");
}
}
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/> <Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>

权限设置

 <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
上一篇:EJB3+JBoss5+Myeclipse9创建HelloWorld实例


下一篇:java多线程之生产者消费者模型