【问题标题】:take photo from camera in service从服务中的相机拍照
【发布时间】:2016-09-22 05:35:53
【问题描述】:

如何在服务中从前置摄像头拍照而不在屏幕上显示摄像头。 我有服务等级

public class PhotoTakingService extends Service {
    //Camera variables
    //a surface holder
    private SurfaceHolder sHolder;
    //a variable to control the camera
    private Camera mCamera;
    //the camera parameters
    private Parameters parameters;

    boolean mPreviewRunning = false;


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public void onStart(Intent intent,int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent,startId);



        mCamera = Camera.open();
        SurfaceView sv = new SurfaceView(getBaseContext());


        try {

            Camera.Parameters p = mCamera.getParameters();
            mCamera.setParameters(p);
            mCamera.startPreview();

            mCamera.takePicture(null,null,mPictureCallback);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        //Get a surface
        sHolder = sv.getHolder();
        //tells Android that this surface will have its data constantly replaced
        sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }


    Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] imageData,Camera c) {
            Log.e("Callback TAG","Here in jpeg Callback");

            if (imageData != null) {
                FileOutputStream outputStream = null;
                try {
                    outputStream = new FileOutputStream("/sdcard/car_final/Image.jpg");
                    outputStream.write(imageData);

                    // Removed the finish call you had here
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (outputStream != null) try {
                        outputStream.close();
                    } catch (IOException ex) {
                        // TODO Auto-generated catch block
                        ex.printStackTrace();
                    }
                }

            }
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
}

和主要活动。我想从主要活动中调用。

 public class MainActivity extends Activity implements SurfaceHolder.Callback {



 private static final String TAG = MainActivity.class.getSimpleName();

    public static SurfaceView mSurfaceView;
    public static SurfaceHolder mSurfaceHolder;
    public static Camera mCamera;
    public static boolean mPreviewRunning;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        Button btnStart = (Button) findViewById(R.id.button1);
        btnStart.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this, PhotoTakingService.class);
                //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startService(intent);

            }
        });

      /*  Button btnStop = (Button) findViewById(R.id.StopService);
        btnStop.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                stopService(new Intent(CameraRecorder.this, RecorderService.class));
            }
        });*/
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    }
}

【问题讨论】:

    标签: android android-camera android-service


    【解决方案1】:

    当您在后台线程(例如服务)中时,使用SurfaceTexture 而不是SurfaceHolder。如果您正在寻找实现 here is a opensource app 我已经实现了背景视频流和 UI 视频流的地方。

    【讨论】:

    • 我只想从前置摄像头捕捉图像.. 不显示摄像头。
    • 类似的情况,您可以从提供的链接中得到想法。
    • @SakibSami 你能再发一次链接吗?这个不行。
    • @YashKrishanVerma 请立即尝试。
    • 谢谢。能否也分享一下接收源?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多