Domanda

I have run into what I think is a device specific issue, but I wanted to see if anyone else has seen this behavior before. Below is the code I am using to capture a JPEG in my camera activity:

surfaceView.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        camera.autoFocus(new AutoFocusCallback() {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                if(success)
                {                           
                    Log.d(TAG, "Focusing...successful.");
                    camera.takePicture(null, null, null, jpegCallback);
                }
                else
                {
                    Log.d(TAG, "Focusing...failed.");
                }

            }

        });
    }
});

This code works perfectly on my Nexus 4, but this afternoon I tried it on a Samsung Galaxy S3, and the camera server crashes with the following error messages:

Focusing...successful.
Camera server died!
ICamera died
Error 100

As you can see from the LogCat, the failure occurs when camera.takePicture(...) is called (i.e., the jpegCallback is never invoked). Later I got a friend with a Samsung Galaxy Note to try it, and it works fine on their phone.

Should I not be taking a picture from inside the auto focus callback? This seems like the appropriate place to put it. Any insight here would be awesome!

È stato utile?

Soluzione

I figured this out a while back, and wanted to let others know how to fix this. What caused the crash was setting the preview size larger than the screen size of the device. This is why the Samsung Galaxy S3 would crash, but not the Nexus 4. Below is the relevant code I changed:

private Camera.Size getBestPreviewSize(List<Camera.Size> previewSizes, int width, int height) {
                double targetAspect = (double)width / (double)height;

        ArrayList<Camera.Size> matchedPreviewSizes = new ArrayList<Camera.Size>();
        final double ASPECT_TOLERANCE = 0.1;
        for(Size previewSize : previewSizes) {
                double previewAspect = (double)previewSize.width / (double)previewSize.height;

                // Original broken code.
                //if(Math.abs(targetAspect - previewAspect) < ASPECT_TOLERANCE) {
                //        matchedPreviewSizes.add(previewSize);
                //}

                // Working code.
                if(Math.abs(targetAspect - previewAspect) < ASPECT_TOLERANCE &&
                            previewSize.width <= width && previewSize.height <= height) {
                        matchedPreviewSizes.add(previewSize);
                }
        }

        Camera.Size bestPreviewSize;
        if(!matchedPreviewSizes.isEmpty()) {
                bestPreviewSize = Collections.max(matchedPreviewSizes, sizeComparator);
        } else {
                bestPreviewSize = Collections.max(previewSizes, sizeComparator);
        }

        return bestPreviewSize;
}

private Comparator<Camera.Size> sizeComparator = new Comparator<Camera.Size>() {

    @Override
    public int compare(Size lhs, Size rhs) {
        long lhsArea = lhs.height*lhs.width;
        long rhsArea = rhs.height*rhs.width;

        if(lhsArea > rhsArea) {
            return 1;
        } else if(lhsArea < rhsArea) {
            return -1;
        } else {
            return 0;
        }
    }

};

In the above code, I show the original broken code along with the working code that caused my function to respect the maximum screen size of the device. Doing this fixed the crashes that were occurring on various devices.

Hope that helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top