在背景Android中使用相机 [英] Using the camera in Background Android

查看:133
本文介绍了在背景Android中使用相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将照片从Camera投入使用.

I'm trying to get the picture into service from Camera.

@Override
public void onCreate() {
    super.onCreate();
    //android.os.Debug.waitForDebugger();

    myCamera=Camera.open();


      SurfaceView dummy=new SurfaceView(getApplicationContext());
    try {
        if(myCamera!=null)
        {
            myCamera.setPreviewDisplay(dummy.getHolder());
            myCamera.setPreviewCallback(this);
            Log.i(TAG,"myCamera is not null");
        }
            getFrames();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Log.e(TAG, "setPreviewDisplay " + e);
    }    
    myCamera.startPreview(); 
}


    public void getFrames() {

        new Thread(new Runnable() {

            public void run() {

                    while(flag)
                    {
                        Log.i(TAG, "getFrames");
                        try{

                            //method();
                        takePictureNoPreview();
                        Thread.sleep(54);

                        } catch (Exception e) {
                            Log.e(TAG, "getFrames thread error: " + e);
                        }
                    }
                     myCamera.release();  

            }
        }).start();
    }


public void takePictureNoPreview(){
            try{
              Log.i(TAG,"takePictureNoPreview");
              myCamera.takePicture(null, null, getJpegCallback())
            } catch (Exception e) {
                Log.e(TAG, "takePictureNoPreview " + e);
            }


     private PictureCallback getJpegCallback(){
                PictureCallback jpeg=new PictureCallback() {   
                  @Override
                  public void onPictureTaken(byte[] data, Camera camera) {
                    try {
                      Log.i(TAG,"getJpegCallback");
                      FileOutputStream(String.format("/sdcard/RealSpeaker/%d.jpg", System.currentTimeMillis()));
                      FileOutputStream os = new FileOutputStream(String.format("/sdcard/Sample/%d.jpg", System.currentTimeMillis()));
                         os.write(data);
                         os.close();
                    }  catch (IOException e) {
                      //do something about it
                    }
                  }
                };
                return jpeg;

              }

问题是周围的getJpegCallback方法不正确:(文件夹中没有日志和图像) 在调试应用程序时,TAG- getJpegCallback 不会显示在LogCat中,但是会显示TAG takePictureNoPreview .关闭应用程序后,相机不允许(现在没问题).怎么了?

The problem is that the method getJpegCallback around is incorrect: (no logs and images in a folder)ю When i'm debugging application, TAG - getJpegCallback do not show in LogCat, but TAG takePictureNoPreview shows. And after closing application, Camera dont allow(now it's no problem). What's wrong?

推荐答案

开发

I faced this issue when developing EyeSpy. I wanted to capture images even app is in background or foreground. I tried for some weeks but no luck. Capturing photo using camera means displaying its preview.

来自文档(5.和6).使用 SurfaceView 来显示预览.

From Document(5. and 6.) And you have to use SurfaceView to display preview.

当您关闭应用程序或您的应用程序进入后台时,SurfaceView的表面将被破坏.这就是为什么我认为这是不可能的.

When you close app or your app goes into background the surface of SurfaceView will be destroyed. That is why I think it's not possible.

如果找到其他方法,请在此处发布.

If you found any other way then please post here.

希望这对您有所帮助.

这篇关于在背景Android中使用相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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