近期公司要搞摄像头测试,就专心在网上找资料,可是网上的资料很多都是只有一些简单的介绍,今天我就整理好,希望可以帮助一下有需要的朋友!!
1、首先的在清单AndroidManifest.xml中添加一下权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2、贴出代码
package com.camera.ui;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class CameraActivity extends Activity {
private SurfaceView mSurfaceView;
private SurfaceHolder mHolder;
private Camera mCamera;
Boolean mBoolean=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initSurface();
new Thread(new Runnable() {
@Override
public void run() {
initCamera();
}
}).start();
}
private void initSurface(){
mSurfaceView = (SurfaceView) findViewById(R.id.camera_surfaceview);
mHolder = mSurfaceView.getHolder();
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void initCamera() {
if(checkCameraHardware(getApplicationContext())){
openFacingCamera();
autoFocus();
}
}
private void autoFocus() {
try {
//Because open camera takes time, let the thread to sleep here
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mCamera.autoFocus(mAutoFocus);
mCamera.takePicture(null, null, mPicCallback);
}
private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
return true;
} else {
return false;
}
}
private Boolean openFacingCamera() {
//Open a front-facing camera
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
if(!mBoolean){
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
mCamera = Camera.open(camIdx);
mBoolean =true;
}
}
}
else if(mBoolean){
//Open the rear camera
for (int camIdx = 0, cameraCount = Camera.getNumberOfCameras(); camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
try {
mCamera = Camera.open(camIdx);
mBoolean =false;
} catch (RuntimeException e) {
e.printStackTrace();
return false;
}
}
}
}
try {
mCamera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
mCamera.stopPreview();
mCamera.release();
mCamera = null;
return false;
}
mCamera.startPreview();
return true;
}
private AutoFocusCallback mAutoFocus = new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean success, Camera camera) {
}
};
private PictureCallback mPicCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//Will get 270 photos of rotation, so that it is vertical
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix mMatrix = new Matrix();
mMatrix.preRotate(270);
mBitmap = Bitmap.createBitmap(mBitmap ,0,0, mBitmap .getWidth(), mBitmap .getHeight(),mMatrix,true);
File mFile = new File(getDir(), "CameraTest.jpg");
try {
FileOutputStream mFileOutputStream = new FileOutputStream(mFile);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, mFileOutputStream);
mFileOutputStream.close();
} catch (Exception error) {
Toast.makeText(CameraActivity.this, "Pictures failed", Toast.LENGTH_SHORT).show();;
error.printStackTrace();
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
Toast.makeText(CameraActivity.this, "To get photos success", Toast.LENGTH_SHORT).show();;
mCamera.stopPreview();
mCamera.release();
mCamera = null;
initCamera();
}
};
private File getDir(){
File mDir = Environment.getExternalStorageDirectory();
if (mDir.exists()) {
return mDir;
} else {
mDir.mkdirs();
return mDir;
}
}
}
3、还有就是简单的main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyCamera" >
<SurfaceView
android:id="@+id/camera_surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</SurfaceView>
</RelativeLayout>
4、参考了一些厉害的博客,自己也在学习积累。
例如:http://blog.csdn.net/bill_ming/article/details/7730305