Kratz
Kratz

Reputation: 4340

Trouble with Android Camera

I have some code I have been experimenting with to see what I can do with the camera device. This following code works, but I have some issues with it that I cannot seem to solve.

  1. The first call never works. The first time running the code the onPictureTaken callback is never called, so the file is never written. However the camera goes through all the other steps, including making the fake shutter noise.

  2. I can't seem to set the picture size to something other than whatever it defaults to. If I try to set it to something else, the code stops working. Does the same as above, where the camera goes through all the motions, but the onPictureTaken callback is never called.

  3. When the pictures are saved to the DCIM folder, the taken pictures do not show up in the default 'Photos' app on my phone, unless i reboot the phone.

  4. Is there any way through code to disable the shutter noise?

Sorry, the code is a little messy because its an experiment.

Also, this code is executed in a BroadcastReceiver

    @Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub


    if(intent.getAction().equals(TAKE_PICTURE_INTENT))
    {


        Toast.makeText(context, "Test", Toast.LENGTH_LONG).show();


                System.out.println("GOT THE INTENT");

                    try
                    {
                        Camera camera = Camera.open();

                        System.out.println("CAMERA OPENED");

                        Parameters params = camera.getParameters();
                        params.set("flash-mode", "off");
                        params.set("focus-mode", "infinity");
                        params.set("jpeg-quality", "100");
                        //params.setPictureSize(2592, 1952);

                        String str = params.get("picture-size" + "-values");
                        System.out.println(str);

                        String size = str.split(",")[0];
                        System.out.println(size);

                        //params.set("picture-size", size);

                        camera.setParameters(params);


                        System.out.println("CAMERA PARAMETERS SET");



                        camera.startPreview();
                        System.out.println("CAMERA PREVIEW STARTED");


                        camera.autoFocus(new AutoFocusCallBackImpl());


                    }
                    catch(Exception ex)
                    {
                        System.out.println("CAMERA FAIL, SKIP");
                        return ;
                    }


    }//if   

}//onreceive


private void TakePicture(Camera camera)
{
    camera.takePicture(new Camera.ShutterCallback() {

        @Override
        public void onShutter() {
            // TODO Auto-generated method stub

            System.out.println("CAMERA SHUTTER CALLBACK");
        }
    }
     , null, 
        new Camera.PictureCallback() {

                public void onPictureTaken(byte[] imageData, Camera c) {
                    //c.release();

                    System.out.println("CAMERA CALLBACK");

                    FileOutputStream outStream = null;
                    try {

                        System.out.println("Start Callback");
                        File esd = Environment.getExternalStorageDirectory();

                        outStream = new FileOutputStream(esd.getAbsolutePath() + String.format(
                                "/DCIM/%d.jpg", System.currentTimeMillis()));

                        outStream.write(imageData);
                        outStream.close();
                        System.out.println( "onPictureTaken - wrote bytes: " + imageData.length);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();

                          System.out.println("File not found exception");
                    } catch (IOException e) {
                        e.printStackTrace();

                          System.out.println("IO exception");
                    } finally {
                          System.out.println("Finally");
                          c.release();
                    }

                }
            }
         );
    //camera.release();
}//TAKE PICTURE

private class AutoFocusCallBackImpl implements Camera.AutoFocusCallback {
    @Override
    public void onAutoFocus(boolean success, Camera camera) {
        //bIsAutoFocused = success; //update the flag used in onKeyDown()
        System.out.println("Inside autofocus callback. autofocused="+success);
        //play the autofocus sound
        //MediaPlayer.create(CameraActivity.this, R.raw.auto_focus).start();

        if(success)
        {
            System.out.println("AUTO FOCUS SUCCEDED");
        }
        else
        {
            System.out.println("AUTO FOCUS FAILED");
        }

        TakePicture(camera);
        System.out.println("CALLED TAKE PICTURE");

    }
 }//AUTOFOCUSCALLBACK

Upvotes: 2

Views: 2974

Answers (1)

Piyush Patel
Piyush Patel

Reputation: 1825

1.First of all put all camera logic out of BroadCast receiver & put it into seprate Activity.

2.

When the pictures are saved to the DCIM folder, the taken pictures do not show up in the default 'Photos' app on my phone, unless i reboot the phone.

because MediaScanner needs to be called to rescan images/changes once you take photo. When u reboot phone mediascanner scans media & finds new images. for this isuue you should check out MediaScanner.

3.Follow Android Camera Tutorial & Camera API

-Thanks

Upvotes: 1

Related Questions