用短信照相机拍照

时间:2013-04-24 18:06:36

标签: android camera sms broadcast

我有这个活动(MakePhotoActivity)类,它在打开应用程序时拍照。我把它设置为使用前置摄像头。

然后我有另一个班级,这是一个已经在工作的短信广播接收器。

现在,我想这样做,以便在接收短信时,我希望它使用我当前的课程拍照。但是我如何将它们整合在一起呢?

由于我已经尝试将方法(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;
        }
    }
}

更新:这就是我所做的。投入服务,但我得到一个错误,说app传递了NULL表面,Camera server死了!,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;
    }

对此有任何帮助的人?感谢。

1 个答案:

答案 0 :(得分:0)

在短信广播接收者的onReceive方法中执行此操作:

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

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

在服务中拍摄图片需要您创建虚拟表面视图。这是一个链接,应该解释如何做到这一点: how to take camera capture without a preview from a service or thread?

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