1.配置权限
<uses-feature android:name="android.hardware.camera" /><!-- 照相机硬件设备特性 -->
<uses-permission android:name="android.permission.CAMERA" /><!-- 照相机权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><!-- 写到外存储设备权限(写到SD卡) -->
2.在布局文件中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.image.MainActivity"
tools:ignore="MergeRootFrame"
android:orientation="vertical">
<!-- 创建出来的预览类添加到FrameLayout里,这里是因为秘密拍摄,将宽高设置成1dp -->
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="1dp"
android:layout_height="1dp"/>
<Button
android:onClick="takePicture"
android:text="拍照"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
3.创建照相机画面预览类
/**
* 照相机画面预览类
* @author Administrator
*
*/
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private static final String TAG = "CameraPreview";
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
//预览界面创建的时候执行
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
//预览界面销毁的时候执行
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
//预览界面旋转改变方向的时候执行
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
4.MainActivity
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.获取照相机实例
mCamera = getCameraInstance();
//2.获取照相机预览类
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);//3.将照相机的预览界面加入到指定的FrameLayout里
}
//当点击按钮的时候进行拍照
public void takePicture(View view){
mCamera.autoFocus(null);//自动对焦,参数是个自动对焦完毕后的回调函数(一般是在这个回调函数里拍照)
/**
* 1.参数一:按下快门的时候的回调函数(一般是调用快门声音)
* 2.参数二:当相机获取原始照片时触发该监听器
* 3.参数三:当相机获取JPG照片时触发该监听器
*/
mCamera.takePicture(null, null, mPicture);
}
/**
* 获取一个照相机实例
* @return
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(0); // 获取后置摄像头 open(1)前置 open(0)和open()都是后置
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private PictureCallback mPicture = new PictureCallback() {
private String TAG = "MainActivity";
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = new File(Environment.getExternalStorageDirectory(),"123.jpg");
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (Exception e) {
Log.d(TAG, "Error accessing file: " + e.getMessage());
}
}
};
protected void onDestroy() {
super.onDestroy();
if(mCamera!=null){
mCamera.stopPreview();//停止预览
mCamera.release();//释放资源
mCamera = null;
}
};
}