Samsung Galaxy S4 , Image gets corrupted while taking from camera

前端 未结 2 362
闹比i
闹比i 2020-12-19 20:35

I\'m using camera on my app. Camera is working perfect on all devices upto Samsung S3 even. Image is correct from all other devices.

While taking image from S4 , ima

相关标签:
2条回答
  • 2020-12-19 20:45

    I've been pulling my hair out over this and I think I found the issue, at least with regards to my app - it's got something to do with the aspect ratio of the preview image versus the captured image.

    In my case, my code was sniffing out the ideal preview size based on the aspect ratio of the screen. The S4 is a 1080p phone, so the preview image was 1920x1080, which is a 16:9 aspect ratio. But my code was hardcoded to capturing a 1600x1200 image, which is 4:3, because that's all I needed. But 1600x1200 is not one of the valid sizes the S4 supports.

    Without setting the size, the S4 captured 4128x3096, which is the maximum size, and is 4:3, but the lines still appeared. Once I told the camera to capture a 16:9 photo, the lines went away. In your case, you might want to adjust the preview's aspect ratio.

    Here's some code which can tell you the available sizes.

        List<Camera.Size> previewSizes = p.getSupportedPreviewSizes();
    
        int i = 1;
    
        for (Size previewSize : previewSizes) {
            Log.v("DebugCamera", "previewSize " + i++ + " width: " + previewSize.width + " height: " + previewSize.height);
        }
    
    0 讨论(0)
  • 2020-12-19 21:01

    Just tried this code on S4 and it works. Try it:

    private Camera.Size getBestPreviewSize(int width, int height)
    {
            Camera.Size result=null;    
            Camera.Parameters p = camera.getParameters();
            for (Camera.Size size : p.getSupportedPreviewSizes()) {
                if (size.width<=width && size.height<=height) {
                    if (result==null) {
                        result=size;
                    } else {
                        int resultArea=result.width*result.height;
                        int newArea=size.width*size.height;
    
                        if (newArea>resultArea) {
                            result=size;
                        }
                    }
                }
            }
        return result;
    
    }
    
    public void surfaceCreated(SurfaceHolder holder) {
        if(myCamera == null){
            myCamera = getCameraInstance();
        try {
            myCamera.setPreviewDisplay(holder);
        } catch (IOException e) {
            e.printStackTrace();
        }
      }
    }
    
    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
        // Surface will be destroyed when we return, so stop the preview.
        if (myCamera != null)
        {
            myCamera.stopPreview();
            myCamera.setPreviewCallback(null);
            myCamera.release();
        }
    } 
    
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //This line helped me set the preview Display Orientation to Portrait
                //Only works API Level 8 and higher unfortunately.
    
        camera.setDisplayOrientation(90);
    
        Camera.Parameters parameters = camera.getParameters();
        Camera.Size size = getBestPreviewSize(width, height);
        parameters.setPreviewSize(size.width, size.height);
        camera.setParameters(parameters);
        camera.startPreview();
    
    }
    
    0 讨论(0)
提交回复
热议问题