当前位置: 首页 > 知识库问答 >
问题:

摄像头:setDisplayOrientation功能不适用于Android 2.3.6的三星Galaxy ACE

廉飞捷
2023-03-14

我试图创建一个用于研究的简单相机应用程序。我阅读了Android摄像头的官方文档,然后开始编码。所以我采取了一些措施

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

2.仅将我的活动锁定为纵向模式。

setRequestedOrientation(ActivityInfo.SCREEN\u ORIENTATION\u PORTRAIT);

3、添加了几个摄像头回调以使我的摄像头正常工作。

  • 表面层。回调,
  • 摄像头。预览回调
  • 自动聚焦回叫
  • ShutterCallback
  • 用于原始图像数据的PictureCallback
  • JPG图像数据的PictureCallback

在surfaceChanged方法中,我正在自定义相机设置。到目前为止,我已经为几乎所有的android设备完成了这项工作

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

但后来我用Android版本2.3.6在三星Galaxy ACE上进行了测试,发现相机显示预览已旋转到横向模式。

所以在放置日志猫/断点之后,我知道下面的方法不适用于这个特定的模型

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

注意:我也在Google和SO上寻找了很多解决方案,但到目前为止没有得到任何运气

  • 此处报告的问题
  • android摄像头表面视图方向
  • Android肖像锁定相机预览
  • Android-相机预览是横向的
  • Android摄像头旋转
  • Android:相机预览旋转
  • 如何正确设置Android摄像头方向

下面是我的示例代码供您参考

@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();
            }
        }
    }

My Camera Activity(MainActivity.java)完整代码为:

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

}

编辑:我在开发者论坛上发表了评论,但没有回应。

请有人对这个问题有任何想法。

如果您能提出任何建议,我将不胜感激。

共有3个答案

陆晓博
2023-03-14

我通过编写一些主要改编自ZXing项目的代码来处理摄像头:http://code.google.com/p/zxing/

你可以试着用一下。如果您正在尝试使用此选项,我建议您仔细阅读此stackoverflow问题的解决方案

这可能不是您问题的完美解决方案,但我已经浏览了这个问题,并决定使用Zxing。

罗乐意
2023-03-14

我也花了几个小时寻找解决方案。真的很疯狂。我的解决方案是只在横向模式下使用相机预览(在我的肖像应用程序中),并掩盖我不需要的预览部分。我用一些视图覆盖了全屏预览的上下部分。当然,你必须裁剪你从相机中获得的照片,这会导致分辨率降低。对于我的应用程序,这不是问题。

我现在对我的解决方案非常满意-用户看不到任何区别;

屠晟睿
2023-03-14

当运行2.2.1的原始Galaxy选项卡出现类似问题时,我可以通过执行以下操作来解决它:

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

然而,看起来您可能已经尝试过这种精确的组合,因为您有上面相同的(但被注释掉的)代码。然而,按照您现在拥有代码的方式,Ace将绕过您拥有注释代码的地方,因为它是API级别(10)。请在此块中特别尝试:

if (currentSDKVersion != 7) { }

如果有用请告诉我谢谢!

 类似资料:
  • 在为inApp相机实施camera2 API后,我注意到在三星设备上图像显得模糊。搜索之后,我找到了Sasmung Camera SDK(http://developer.samsung.com/galaxy#camera)。因此,在三星Galaxy S7上实施SDK后,图像现在很好,但在Galaxy S6上它们仍然模糊不清。有人在三星设备上遇到过这些问题吗? 编辑:补充@rcsumners的评论

  • 我试图使用官方openCV教程中的代码,在Ubuntu/Python 3.6中使用显示网络摄像头的视频: 对于: 功能未实现。使用Windows、GTK 2. x或Carbon支持重建库。如果你在Ubuntu或Debian上,安装libgtk2.0-dev和pkg-config,然后在cvShowImage函数中重新运行cmake或配置脚本 在搜索错误时,我偶然发现了这篇文章,作为类似问题的替代答

  • 表情测试 图片转文字 相框 拍摄图片

  • 摄像头用于采集图像和影像信息,通过模块间的组合完成各种创意活动。 净重量:8.1g 体积:24×24×22mm 参数 分辨率:1280×720 像素大小:3.4um×3.4um 最大图像传输速率:全尺寸 @ 30fps 视场角:100° 镜片结构:4G+IR 焦比:2.97 有效焦距:2.4mm 功耗:100uA(待机)~240mW(工作) 电源:USB总线电源 抗跌落能力:1m 工作温度:-30

  • 我已创建WebView活动并正在加载https://web.doar.zone/c冠状病毒 这个URL需要相机权限,这是我在Android中获得的运行时权限。 下面是mainactivity.java的完整代码:

  • camera 对象提供对设备默认摄像头应用程序的访问。 方法: camera.getPicture 参数: cameraSuccess cameraError cameraOptions camera.getPicture 选择使用摄像头拍照,或从设备相册中获取一张照片。图片以base64编码的字符串或图片URI形式返回。 简单的范例: navigator.camera.getPicture( c