Android开发之摄像头操作

    想要开发Android摄像头相关应用?本文将会对你有所帮助。

    首先,需要你编写一个摄像头预览类,我将它命名为CameraPreview,需要集成SurfaceView,并且实现Callback。代码如下:

    public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{


private static final String TAG = null;
private SurfaceHolder holder;
private Camera mCamera;

@SuppressWarnings("deprecation")
public CameraPreview(Context context,Camera camera) {
super(context);
mCamera = camera;
// 获取回调函数
// 添加回调函数
holder = getHolder();
holder.addCallback(this);
// 此步骤已经不需要
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 捕获异常
try{
           mCamera.setPreviewDisplay(holder);
           mCamera.startPreview();
}catch(IOException e){
Log.d(TAG,"Error setting camera preview: "+ e.getMessage());
}
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
if(holder.getSurface()==null){
return;
}
try{
mCamera.stopPreview();
}catch(Exception e){
}
try{
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
}
catch(Exception e){
Log.d(TAG,"Error starting camera preview: "+ e.getMessage());
}

}

public void surfaceStop(){
try{
mCamera.stopPreview();
}catch(Exception e){
}
}

public void surfaceStart(){
try{
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
}
catch(Exception e){
Log.d(TAG,"Error starting camera preview: "+ e.getMessage());
}
}


@Override
public void surfaceCreated(SurfaceHolder arg0) {
// TODO Auto-generated method stub

}


@Override
public void surfaceDestroyed(SurfaceHolder arg0) {

}
}

  接下来就可以新建一个带有摄像头View的Activity了。

   在AndroidMainfest.xml里面加上:

   <activity android:name="com.cleaderwin.home.dp." />

还有相关权限:

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


    <uses-feature
        android:name="android.hardware.camera"
        android:required="false" />

.xml里面加上

<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="800dp"
android:layout_height="520dp"
android:layout_weight="1"
/>


接下来就是VideoActivity.java的代码:

public class VideoActivity extends Activity implements OnClickListener {
private CameraPreview mPreview;
private static Camera camera;
private boolean isCameraOpen = false;

@Override
protected void onPause() {
// TODO Auto-generated method stub
// Button btn = (Button)findViewById(R.id.button_capture);
isCameraOpen = false;
mPreview.surfaceStop();
// btn.setText("打开摄像头");
super.onPause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
// Button btn = (Button)findViewById(R.id.button_capture);
isCameraOpen = true;
mPreview.surfaceStart();
// btn.setText("打开摄像头");
super.onResume();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
if (checkCameraHardware(this) == false) {
Toast.makeText(this.getApplicationContext(), "没有检测到摄像头,请检查是否有程序正在使用摄像头!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this.getApplicationContext(), "检测到摄像头,开启摄像头!", Toast.LENGTH_LONG).show();
// 获取camera句柄
camera = getCameraInstance();
// 初始化摄像头预览
mPreview = new CameraPreview(this, camera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
isCameraOpen = true;
}
}

private boolean checkCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}

public static Camera getCameraInstance() {
if (camera == null) {
Camera c = null;
try {
c = Camera.open(); // 打开摄像头
} catch (Exception e) {
// 处理打不开的异常
}
return c;
} else {
return camera;
}
}

@Override
public void onClick(View arg0) {
// Toast.makeText(this.getApplicationContext(), "XXXXX", Toast.LENGTH_LONG).show();
Button btn = (Button) findViewById(R.id.button_capture);
if (isCameraOpen == true) {
isCameraOpen = false;
mPreview.surfaceStop();
btn.setText("打开摄像头");
} else {
isCameraOpen = true;
mPreview.surfaceStart();
btn.setText("关闭摄像头");
}
}

}

    赶紧体验一下摄像头操作吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值