0

我有这个活动(MakePhotoActivity)类,可以在您打开应用程序时拍照。我已将其设置为使用前置摄像头。

然后我有另一个类,它是一个已经在工作的 SMS 广播接收器。

现在,我想让它在收到短信时,使用我当前的班级拍照。但是我怎样才能将它们整合在一起呢?

当我尝试将方法(surfaceChanged 等)复制到我的广播接收器类时,在接收短信时,我将代码放在 onCreate 内(在 MakePhotoActivity 中)。它不工作。

public class MakePhotoActivity extends Activity implements SurfaceHolder.Callback
{
    //a variable to store a reference to the Image View at the main.xml file
    private ImageView iv_image;
    //a variable to store a reference to the Surface View at the main.xml file
    private SurfaceView sv;

    //a bitmap to display the captured image
    private Bitmap bmp;
    private int cameraId = 0;

    //Camera variables
    //a surface holder
    private SurfaceHolder sHolder; 
    //a variable to control the camera
    private Camera mCamera;
    //the camera parameters
    private Parameters parameters;


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


        //get the Image View at the main.xml file
        iv_image = (ImageView) findViewById(R.id.imageView);

        //get the Surface View at the main.xml file
        sv = (SurfaceView) findViewById(R.id.surfaceView);

        //Get a surface
        sHolder = sv.getHolder();

        //add the callback interface methods defined below as the Surface   View callbacks
        sHolder.addCallback(this);

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

    private int findFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d("A", "Camera found");

                cameraId = i;
                break;
            }
        }
        return cameraId;
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
    {
        //get camera parameters
        parameters = mCamera.getParameters();

        //set camera parameters
        mCamera.setParameters(parameters);
        mCamera.startPreview();

        //sets what code should be executed after the picture is taken
        Camera.PictureCallback mCall = new Camera.PictureCallback()
        {

            public void onPictureTaken(byte[] data, Camera camera)
            {
                //decode the data obtained by the camera into a Bitmap
                bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                //set the iv_image
                iv_image.setImageBitmap(bmp);
                FileOutputStream outStream = null;
                try{
                    outStream = new FileOutputStream("/sdcard/Image"+System.currentTimeMillis()+".jpg");
                    outStream.write(data);
                    outStream.close();
                } catch (FileNotFoundException e){
                    Log.d("CAMERA", e.getMessage());
                } catch (IOException e){
                    Log.d("CAMERA", e.getMessage());
                }
            }
        };

        mCamera.takePicture(null, null, mCall);
    }


    public void surfaceCreated(SurfaceHolder holder)
    {
        // The Surface has been created, acquire the camera and tell it where
        // to draw the preview.

        if (mCamera == null) {
            cameraId = findFrontFacingCamera();
            mCamera = Camera.open(cameraId);
            try {
                mCamera.setPreviewDisplay(holder);

                // TODO test how much setPreviewCallbackWithBuffer is faster
                // mCamera.setPreviewCallback((PreviewCallback) this);
            } catch (IOException e) {
                mCamera.release();
                mCamera = null;
            }
        }
    }


    public void surfaceDestroyed(SurfaceHolder holder)
    {
        if (mCamera != null) {
            mCamera.stopPreview();
            mCamera.setPreviewCallback(null);
            mCamera.release();
            mCamera = null;
        }
    } 

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mCamera!=null)
        {
            mCamera.stopPreview();
            mCamera.release();
            mCamera=null;
        }
    }
}

更新:这就是我所做的。投入服务,但我得到一个错误,说应用程序通过了 NULL 表面,相机服务器死了!,ICamera 死了,错误 100。

我引用了http://easyandroidtutorials.blogspot.in/2012/09/capture-image-without-preview-as.html中的代码并做了一些小改动,仍然无法工作。

public class CameraService 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;
    SurfaceView sv;
    private int cameraId = 0;
    /** Called when the activity is first created. */
    @Override
    public void onCreate()
    {
        super.onCreate();
        Log.i("Service", "Service started");




    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        try {


                if (mCamera == null) {
                    cameraId = findFrontFacingCamera();
                    mCamera = Camera.open(cameraId);

                try {
                    // Thread.sleep(3000);
                    sv = new SurfaceView(getApplicationContext());
                    mCamera.setPreviewDisplay(sv.getHolder());
                    parameters = mCamera.getParameters();

                    //set camera parameters
                    mCamera.setParameters(parameters);
                    mCamera.startPreview();
                    mCamera.takePicture(null, null, mCall);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    mCamera.release();
                    mCamera = null;
                    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);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Service.START_STICKY;
    }

    private int findFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                Log.d("A", "Camera found");

                cameraId = i;
                break;
            }
        }
        return cameraId;
    }

    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());
            }

        }
    };


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

有人对此有任何帮助吗?谢谢。

4

1 回答 1

0

在您的 sms 广播接收器的 onReceive 方法中执行以下操作:

Intent intent = new Intent(this, MakePhotoActivity.class);
startActivity(intent);

检查http://developer.android.com/training/basics/firstapp/starting-activity.html以获取有关启动活动的更多信息

要在服务中拍照,您需要创建一个虚拟表面视图。这是一个应该解释如何做到这一点的链接: 如何在没有从服务或线程预览的情况下进行相机捕捉?

如果你想禁用快门声音:camera.enableShutterSound(false); http://developer.android.com/reference/android/hardware/Camera.html#enableShutterSound(boolean)

于 2013-04-24T18:25:09.987 回答