Question

I am working on a hidden camera application but I have some problems with taking photo.

When I debug the application, I can see that takePicture can be invoked but it doesn't call pictureCallback class.

I am putting class I am using and waiting for your helps.

public class TakePhotoClass {

private final String LOG_TAG="Photo Taker Class";      

private Context context;

private Camera cam;
private Camera.PictureCallback pictureCallback;

private AudioManager audioManager;

public TakePhotoClass(Context con) {
    // TODO Auto-generated constructor stub

    context=con;

    audioManager=(AudioManager) con.getSystemService(Context.AUDIO_SERVICE);
}

public void takePhoto(){
    prepareCamera();

    audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, true);

    try{
        cam.startPreview();
        cam.takePicture(null, null, pictureCallback);

    }catch(Exception ex)
    {
        Log.e(LOG_TAG, "Couldn't be taken photo. Error message: "+ex.getLocalizedMessage());
    }

}

private void prepareCamera(){
    cam=Camera.open();

    Camera.Parameters param=cam.getParameters();

    param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
    param.setJpegQuality(100);
    param.setFocusMode(param.FOCUS_MODE_AUTO);
    param.setPictureFormat(ImageFormat.JPEG);

    cam.setParameters(param);


    SurfaceView view=new SurfaceView(context);

    try {
        cam.setPreviewDisplay(view.getHolder());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e(LOG_TAG, "Can not set surface");
    }


    setCallback();

}

private void setCallback(){
    pictureCallback=new Camera.PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            // TODO Auto-generated method stub

            try{

                File parentFolder=new File(Environment.getExternalStorageDirectory()+"/Agent Cam/Photo");

                File photoFile=new File(parentFolder,"photo_"+getPhotoTime()+".jpg");

                if(!photoFile.exists())
                    photoFile.mkdirs();

                FileOutputStream stream=new FileOutputStream(photoFile);

                stream.write(data);
                stream.flush();
                stream.close();

                audioManager.setStreamMute(AudioManager.STREAM_SYSTEM, false);

                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+Environment.getExternalStorageDirectory())));

                camera.release();
            }catch(Exception ex)
            {
                Log.e(LOG_TAG, "Photo coluldn't be saved. Error message: "+ex.getLocalizedMessage());
            }
        }
    };
}

private String getPhotoTime(){
    SimpleDateFormat sdf=new SimpleDateFormat("ddMMyy_hhmmss");

    return sdf.format(new Date());
}   

}
Was it helpful?

Solution

Camera.PictureCallback's method onPictureTaken is called asynchronously (see takePicture), your app should wait for it to be called.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top