本文介绍了摄像头:setDisplayOrientation功能不工作的三星Galaxy ACE采用Android 2.3.6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个研究简单的摄像头应用程序。我阅读 Android摄像头公文,然后开始编码。所以我做了一些步骤,以得到它的工作

I was trying to create a simple camera application for research.I Read Android Camera Official Document and then started coding.so I did some steps to get it work

1.添加应用程序中的照相机功能所需的权限。

1.Added required permissions for Camera feature in app.

2.locked我的活动只肖像模式。

2.locked my activity to PORTRAIT mode only.

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

3.Added几个摄像头的回调让我的相机的作品。

3.Added several camera callbacks to get my camera works.

  • SurfaceHolder.Callback,
  • 相机。previewCallback
  • AutoFocusCallback
  • ShutterCallback
  • PictureCallback对RAW图像数据
  • PictureCallback的JPG图像数据

4.In surfaceChanged 方法,我自定​​义相机设置。而到目前为止,我得到了这个工作,几乎所有的Andr​​oid设备

4.In surfaceChanged method I am customizing camera settings.and so far I got working this for almost all android devices

  • LG
  • 香料
  • 三星
  • HTC
  • 福邦
  • 索尼
  • 摩托罗拉
  • 在谷歌Nexus系列。

但是,然后我测试了三星Galaxy ACE 与Android版本2.3.6发现摄像头显示preVIEW旋转为横向模式。

But then I tested on Samsung Galaxy ACE with Android Version 2.3.6and found Camera Display Preview is Rotated to landscape mode.

所以,把日志的猫/破发点我才知道,以下方法不是在为这个特殊的模型

So after putting log-cat/break points I come to know that below methods are not working for this particular model

//This method is not working for Samsung Galaxy ACE
 camera.setDisplayOrientation(90);
//or
parameters.set("orientation", "portrait");
//or
parameters.setRotation(90);

注意: 我搜遍了一堆谷歌的解决方案等等,以及,但至今没有得到任何运气在这个

  • Issus reported here
  • android camera surfaceview orientation
  • Android portrait locked Camera Preview
  • Android - Camera preview is sideways
  • Android camera rotate
  • Android: Camera preview Rotated
  • How to set Android camera orientation properly?

有关您参考我的例子code低于

For your reference My sample code is below

@Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Log.v(TAG, "surfaceChanged get called");

        if (previewing) {
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null) {
            try {
                Camera.Parameters parameters = camera.getParameters();
                List<Size> sizes = parameters.getSupportedPictureSizes();

                Camera.Size result = null;
                for (int i = 0; i < sizes.size(); i++) {
                    result = (Size) sizes.get(i);
                    Log.i("PictureSize", "Supported Size. Width: "
                            + result.width + "height : " + result.height);

                    if (result.width == 640) {
                        parameters.setPreviewSize(result.width, result.height);// 640*480
                        parameters.setPictureSize(result.width, result.height);
                    } else {

                    }
                }


                //**************************************************************//

                /*
                 * Here is the logic I have added to Manage Camera Display Orientation
                 *  It checks Current Orientation and SDK and then rotate display to make it Portrait view.
                 */
                int currentSDKVersion = android.os.Build.VERSION.SDK_INT;
                Log.d(TAG, "currentVersion " + currentSDKVersion);

                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    if (currentSDKVersion != 7) {

                        camera.setDisplayOrientation(90);
                         parameters.setRotation(90);
                    } else {
                            parameters.setRotation(90);

                        /*
                         * params.set("orientation", "portrait");
                         * params.set("rotation",90);
                         */
                    }
                } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    if (currentSDKVersion != 7) {

                        camera.setDisplayOrientation(0);
                    } else {

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

                    }
                }


                //**************************************************************//

                camera.setParameters(parameters);
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                camera.autoFocus(myAutoFocusCallback);
                camera.setOneShotPreviewCallback(cameraPreviewCallback);
                previewing = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

我的相机活动(MainActivity.java)全部code是:

My Camera Activity (MainActivity.java) Full code is:

public class MainActivity extends Activity implements SurfaceHolder.Callback,
        OnClickListener {

    public static final String TAG = "CameraActivity";
    private Context context = null;
    Camera camera = null;
    private SurfaceView surfaceView = null;
    private SurfaceHolder surfaceHolder = null;
    boolean previewing = false;
    private LayoutInflater controlInflater = null;
    private Button buttonTakePicture = null;
    //private TextView textViewResultInfo = null;

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Log.v(TAG, "onCreate get called");

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //textViewResultInfo = (TextView) findViewById(R.id.textViewResultInfo);
        context = this;

        getWindow().setFormat(PixelFormat.UNKNOWN);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        controlInflater = LayoutInflater.from(getBaseContext());
        View viewControl = controlInflater.inflate(R.layout.control, null);
        LayoutParams layoutParamsControl = new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        this.addContentView(viewControl, layoutParamsControl);

        buttonTakePicture = (Button) findViewById(R.id.takepicture);
        buttonTakePicture.setOnClickListener(this);

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {

        Log.v(TAG, "surfaceCreated get called");
        camera = Camera.open();

        if (camera != null) {
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                previewing = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        Log.v(TAG, "surfaceChanged get called");

        if (previewing) {
            camera.stopPreview();
            previewing = false;
        }

        if (camera != null) {
            try {

                 //new MainActivity().setCameraDisplayOrientation();

                Camera.Parameters parameters = camera.getParameters();
                // List<String> focusModes =
                // parameters.getSupportedFocusModes();
                // if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
                // {
                // parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                // }

                List<Size> sizes = parameters.getSupportedPictureSizes();

                Camera.Size result = null;
                for (int i = 0; i < sizes.size(); i++) {
                    result = (Size) sizes.get(i);
                    Log.i("PictureSize", "Supported Size. Width: "
                            + result.width + "height : " + result.height);

                    if (result.width == 640) {
                        parameters.setPreviewSize(result.width, result.height);// 640*480
                        parameters.setPictureSize(result.width, result.height);
                    } else {

                    }
                }


                //**************************************************************//

                Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
                int rotation = display.getRotation();
                Log.v(TAG, "Current Device Orientation is ="+rotation);

                /*
                 * Here is the logic I have added to Manage Camera Display Orientation
                 *  It checks Current Orientation and SDK and then rotate display to make it Portrait view.
                 */
                int currentSDKVersion = android.os.Build.VERSION.SDK_INT;
                Log.d(TAG, "currentVersion " + currentSDKVersion);

                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    if (currentSDKVersion != 7) {
                        Log.i(TAG, "ORIENTATION_PORTRAIT +SDK is: " + currentSDKVersion
                                + "rotate 90");
                        camera.setDisplayOrientation(90);
                         parameters.setRotation(90);
                    } else {
                        Log.i(TAG, "ORIENTATION_PORTRAIT +SDK is: " + currentSDKVersion
                                + "rotate 90");
                        parameters.setRotation(90);

                        /*
                         * params.set("orientation", "portrait");
                         * params.set("rotation",90);
                         */
                    }
                } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    if (currentSDKVersion != 7) {
                        Log.i(TAG, "ORIENTATION_LANDSCAPE +SDK is: " + currentSDKVersion
                                + "rotate 90");

                        camera.setDisplayOrientation(0);
                    } else {

                        Log.i(TAG, "ORIENTATION_LANDSCAPE +SDK is: " + currentSDKVersion
                                + "rotate 90");

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

                    }
                }


                //**************************************************************//


                camera.setParameters(parameters);
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
                camera.autoFocus(myAutoFocusCallback);
                camera.setOneShotPreviewCallback(cameraPreviewCallback);
                previewing = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.v(TAG, "surfaceDestroyed get called");
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;

    }


    public void setCameraDisplayOrientation()
    {
        Log.v(TAG, "setCameraDisplayOrientation get called");

         if (camera == null)
         {
             Log.d(TAG,"setCameraDisplayOrientation - camera null");
             return;
         }

         Camera.CameraInfo info = new Camera.CameraInfo();
         Camera.getCameraInfo(1, info);

         WindowManager winManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
         int rotation = winManager.getDefaultDisplay().getRotation();

         int degrees = 0;

         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;
         }

         int result;
         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);
    }


    @Override
    public void onClick(View v) {
        Log.v(TAG, "onClick get called");

        if (v == buttonTakePicture) {
            camera.takePicture(myShutterCallback, myPictureCallback_RAW,
                    myPictureCallback_JPG);
        }

    }

    private Camera.PreviewCallback cameraPreviewCallback = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            Log.i(TAG, "onPreviewFrame size=" + data.length);
        }
    };

    AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback() {

        @Override
        public void onAutoFocus(boolean arg0, Camera arg1) {
            Log.v(TAG, "onAutoFocus get called");
            buttonTakePicture.setEnabled(true);
        }
    };

    ShutterCallback myShutterCallback = new ShutterCallback() {

        @Override
        public void onShutter() {
            Log.v(TAG, "onShutter get called");
        }
    };

    PictureCallback myPictureCallback_RAW = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            Log.v(TAG, "onPictureTaken-RAW get called");

        }
    };

    public static Bitmap RotateBitmap(Bitmap source, float angle) {
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        return Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                source.getHeight(), matrix, true);
    }

    PictureCallback myPictureCallback_JPG = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] arg0, Camera arg1) {
            Bitmap rawImage = BitmapFactory.decodeByteArray(arg0, 0,
                    arg0.length);

            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                Log.v(TAG, "#####  ORIENTATION_PORTRAIT   ####");

                rawImage = MainActivity.RotateBitmap(rawImage, 90);

                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                rawImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                arg0 = stream.toByteArray();

            } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                Log.v(TAG, "#####   ORIENTATION_LANDSCAPE  #####");

            }

            Intent intent = new Intent(MainActivity.this, ResultActivity.class);
            intent.putExtra("picture", arg0);
            startActivity(intent);

            Log.v(TAG, "onPictureTaken-JPG get called");

        }
    };

    /**
     * Get the size in bitmap.
     *
     * @param bitmap
     * @return size in bytes
     */
    @TargetApi(12)
    public static int getBitmapSize(Bitmap bitmap) {
        if (MainActivity.hasHoneycombMR1()) {
            return bitmap.getByteCount();
        }
        // Pre HC-MR1
        return bitmap.getRowBytes() * bitmap.getHeight();
    }

    public static boolean hasHoneycombMR1() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1;
    }

}

修改:在<一个我已经发布评论href="https://$c$c.google.com/p/android/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=1193"相对=nofollow>开发者论坛,但没有任何反应。

Edit : I have posted Comment on Developer forum but no response.

请!有人有关于这个问题的任何想法。

Please!! Someone have any Idea regarding this problem.

我真的AP preciate任何一种暗示。

I would really appreciate for any kind of suggestion.

推荐答案

当我有类似的问题与原来的Galaxy Tab运行2.2.1,我能够通过执行以下操作来解决这个问题:

When I had a similar problem with the original Galaxy Tab running 2.2.1, I was able to solve it by doing the following:

Camera.Parameters parameters = camera.getParameters();
parameters.set("orientation", "portrait");
parameters.setRotation(90);
camera.setParameters(parameters);

不过,它看起来像你的可以的已经尝试了确切的组合,因为你有上述相同的(但被注释掉)code。但是,你有code,现在的方式,王牌是要绕过,你有评论code,因为它的API级别(10)。具体试试这个块中:

However, it looks like you may have already tried that exact combination, given that you have identical (but commented out) code above. However, the way you have the code right now, the Ace is going to bypass where you have that commented code because of it's API level (10). Try it specifically inside this block:

if (currentSDKVersion != 7) { }

让我知道,如果它的工作原理,请。谢谢!

And let me know if it works, please. Thanks!

这篇关于摄像头:setDisplayOrientation功能不工作的三星Galaxy ACE采用Android 2.3.6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-11 22:18