4

I am facing problem for displaying camera preview in android 2.3.6 device.I have used this code for changing camera orientation but for 2.3.6 it is not working and for other versions it is working properly.

In device 2.2.1

In device 2.3.6

First image in device 2.2.1,Second image in device 2.3.6.I want camera preview as same as in 2.2.1 for device 2.3.6.Below is my code

if (Integer.parseInt(Build.VERSION.SDK) >= 8)
            {
         mCamera.setDisplayOrientation(90);


       }
        else
        {
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {

               p.set("orientation", "portrait");
               p.set("rotation", 90);

            }
           if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {

                p.set("orientation", "landscape");
                p.set("rotation", 90);

            }
     }   

And i have specified screen orientation portrait in manifest.Kindly suggest me a solution.Thanks in advance.

4

1 に答える 1

0

以下のコードを参照してください。

private void setCameraDisplayOrientation(int cameraId, android.hardware.Camera camera) {
    int rotation = getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    int result;

    if(Build.VERSION.SDK_INT>10){
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
        }

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);

    }else{
        if(cameraId==CAMERA_FACING_BACK){
            camera.setDisplayOrientation(90);
        }else{
            camera.setDisplayOrientation(270);
        }
    }
}

を使用してカメラIDを取得できます

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private Camera openFrontFaceCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) {
            try {
                mCameraId=camIdx;
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
private Camera openRearFaceCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
        Camera.getCameraInfo( camIdx, cameraInfo );
        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK  ) {
            try {
                mCameraId=camIdx;
                cam = Camera.open( camIdx );
            } catch (RuntimeException e) {
                Log.e("Custom Camera", "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}
于 2014-01-27T11:04:32.037 に答える