【问题标题】:Creating an android app using camera api使用相机 api 创建一个 android 应用程序
【发布时间】:2015-02-07 09:18:40
【问题描述】:

我是 android devlopment 的新手。 我正在制作一个相机应用程序,我卡在网上已经有很多解决方案,但我的问题没有解决,请帮忙。 我正在自动捕获图像并将其上传到服务器上但是我以正确的方向以横向模式获取所有图像,否则方向不正确。在预览时,我的显示器正在向右旋转,但捕获图像的图像方向不正确我的代码是

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {

    // Now that the size is known, set up the camera parameters and begin
    // the preview.
    try {
        Camera.Parameters parameters= camera.getParameters();           
         parameters = camera.getParameters();            
        //camera.setParameters(parameters);             
        setCameraDisplayOrientation(Preview.this,0, camera, parameters);
        //camera.setParameters(parameters);
        parameters.getPictureSize();
        parameters.setPictureSize(w, h);
        parameters.getPreviewSize();
        parameters.setPreviewSize(w, h);            
        camera.startPreview();
        } catch(Exception e) {
        Log.d(TAG, "Cannot start preview", e);    
    }
}

public static void setCameraDisplayOrientation(Activity activity,
        int cameraId, android.hardware.Camera camera, Camera.Parameters parameters {android.hardware.Camera.CameraInfo info =
        new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().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;
        }
        //parameters.setRotation(result);
        camera.setDisplayOrientation(result);           
        camera.setParameters(parameters);
        }

【问题讨论】:

    标签: android-camera


    【解决方案1】:

    在调用 camera.setParameters 之前,您需要使用正确的值调用 parameters.setRotation。这会调整捕获的 JPEG 图像的方向,这与预览显示的方向无关。

    有关如何正确调用 setRotation() 的示例代码,请参阅 Android 文档: setRotation documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 2013-07-15
      相关资源
      最近更新 更多