如何在Android应用程序的图片,而用户界面..? [英] How to take pictures in android application without the user Interface..?

查看:139
本文介绍了如何在Android应用程序的图片,而用户界面..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设计一个应用程序,该应用程序必须采取从摄像机画面而不会在布局并没有任何用户界面(如点击一个按钮....)的显示,并储存了吗?

解决方案

 相机mCamera;
私人布尔safeCameraOpen(INT ID){
布尔QOPENED = FALSE;

    尝试 {
        releaseCamera();
        mCamera = Camera.open(ID);
        QOPENED =(!mCamera = NULL);
    }赶上(例外五){
        Log.e(的getString(R.string.app_name),无法打开摄像机);
        e.printStackTrace();
    }

    返回QOPENED;
}

私人无效releaseCamera(){
    如果(mCamera!= NULL){
        mCamera.release();
        mCamera = NULL;
    }
}

//放在你的code调用这样的:
mCamera.takePicture(NULL,NULL,MCALL);


Camera.PictureCallback MCALL =新Camera.PictureCallback(){

   公共无效onPictureTaken(byte []的数据,摄像头摄像头){
         //德$ C C相机获得的成位图数据$

         FileOutputStream中outStream = NULL;
              尝试 {
                  outStream =新的FileOutputStream(/ SD卡/ image.jpg文件);
                  outStream.write(数据);
                  outStream.close();
              }赶上(FileNotFoundException异常E){
                  Log.d(照相机,e.getMessage());
              }赶上(IOException异常E){
                  Log.d(照相机,e.getMessage());
              }

   }
};
 

I have to design an application, that application has to take the picture from the camera without displaying it in the layout and without any user Interface (like click of a button....) and store the picture?

解决方案

Camera mCamera; 
private boolean safeCameraOpen(int id) {
boolean qOpened = false;

    try {
        releaseCamera();
        mCamera = Camera.open(id);
        qOpened = (mCamera != null);
    } catch (Exception e) {
        Log.e(getString(R.string.app_name), "failed to open Camera");
        e.printStackTrace();
    }

    return qOpened;    
}

private void releaseCamera() {
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}

//somewhere in your code call this:
mCamera.takePicture(null, null, mCall);


Camera.PictureCallback mCall = new Camera.PictureCallback() {

   public void onPictureTaken(byte[] data, Camera camera) {
         //decode the data obtained by the camera into a Bitmap

         FileOutputStream outStream = null;
              try {
                  outStream = new FileOutputStream("/sdcard/Image.jpg");
                  outStream.write(data);
                  outStream.close();
              } catch (FileNotFoundException e){
                  Log.d("CAMERA", e.getMessage());
              } catch (IOException e){
                  Log.d("CAMERA", e.getMessage());
              }

   }
};

这篇关于如何在Android应用程序的图片,而用户界面..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆