Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

android - PictureCallback.onPictureTaken never called

I am writing an application that needs pictures taken with the camera. The problem occurs when I try to take an actual picture. Here's the code that is troubling me:

    final ShutterCallback shutterCallback = new ShutterCallback() {
        @Override
        public void onShutter() {
            Log.d(TAG, "onShutter");
        }
    };

    final PictureCallback callback = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            Log.d(TAG, "onPictureTaken - jpeg");
            try {
                //async task for storing the photo
                new SavePhotoTask(CameraView.this.ctx, data).execute();
            } catch (final SavePhotoException e) {
                //some exceptionhandling
            }
        }
    };
    this.camera = Camera.open();
    this.camera.setPreviewDisplay(surfaceHolder);
    final Camera.Parameters parameters = findBestParameters(w, h);
    this.camera.setParameters(parameters);
    this.camera.startPreview();
    Log.d(TAG, "takePicture now!");
    this.camera.takePicture(shutterCallback, null, callback);

On the emulator it seems to work out but on my phone (Motorola Defy - android 2.1).

The actual problem: on the phone the picturecallback is never happening "onPictureTaken" gets never called. The Shuttercallback is executed but the other is not (and I tried with raw instead of jpeg, same thing).

Does anyone know this problem? I just don't see where the difference lies to the emulator right now. I appreciate your help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I finally went and debugged the problem. All of a sudden it worked, because debugging is much slower: It's a timing problem. The callback takes some time to be called. While debugging the phone had enough time to finish taking the picture ...

Also don't go calling Camera.stopPreview() and Camera.release() too early.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...