簡體   English   中英

如何在android中檢測相機方向?

[英]How to detect camera orientation in android?

當我在我的 Android One 手機或模擬器上測試我的應用程序時,一切正常,但在其他一些設備上出現問題。

問題是,我基本上發送了一個相機意圖,在用戶拍照后從意圖中獲取數據,並使用我從相機獲得的任何內容設置ImageView的像素。 對於某些設備(主要是三星)圖像被旋轉,它不會在拍攝時顯示。

我的應用程序只能在縱向模式下工作,但如果用戶在拍攝圖像時旋轉手機,他/她也可以在橫向模式下拍照。

有沒有辦法檢測設備旋轉圖像的默認角度,所以我在拍攝圖像后旋轉位圖?

這是代碼:

發送意圖:

File path = new File(getActivity().getFilesDir(), "map_roomie");

if (!path.exists()) path.mkdirs();

mFileName = createImageFileName();

File image   = new File(path, mFileName);
Uri imageUri = FileProvider.getUriForFile(getActivity(), AddRoomFragment.CAPTURE_IMAGE_FILE_PROVIDER, image);

Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                                         imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                                            startActivityForResult(imageCaptureIntent, AddRoomFragment.CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

捕捉片段意圖

File path = new File(getActivity().getFilesDir(), "map_roomie");

if (!path.exists()) path.mkdirs();

File imageFile = new File(path, mFileName);

setBitmapOfImageView(mCurrentPhotoId, decodeAndReturnBitmap(imageFile.getAbsolutePath()));

輔助功能:

public void setBitmapOfImageView(int photoId, Bitmap bitmap)
{
    mPhotos[photoId].setImageBitmap(bitmap);

    mPhotosState[photoId] = 1;
}

public Bitmap decodeAndReturnBitmap(String filePath)
{
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds    = true;

    BitmapFactory.decodeFile(filePath, o);

    final int REQUIRED_SIZE = 512;

    int widthTemp = o.outWidth, heightTemp = o.outHeight;
    int scale     = 1;

    while (true) {
        if (widthTemp < REQUIRED_SIZE && heightTemp < REQUIRED_SIZE) {
            break;
        }

        widthTemp  /= 2;
        heightTemp /= 2;
        scale      *= 2;
    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize          = scale;

    return BitmapFactory.decodeFile(filePath, o2);
}

如果要使相機圖像以與顯示器相同的方向顯示,可以使用以下代碼。

public static void setCameraDisplayOrientation(Activity activity,
             int cameraId, android.hardware.Camera camera) {
         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;
         }
         camera.setDisplayOrientation(result);
     }

有關更多信息,請參閱https://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

這個解決方案適合我。

public static void setCameraDisplayOrientation (Activity activity, int cameraId, android.hardware.Camera camera){
    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;
    }
    camera.setDisplayOrientation(result);
}

請看一看 -

    private int getCameraId() {
        int curCameraId = 0;

        if (Camera.getNumberOfCameras() > 0) {
            curCameraId = (curCameraId + 1) % Camera.getNumberOfCameras();
        } else {
            curCameraId = 0;
        }
        return curCameraId;
    }

    public void setCameraDisplayOrientation(Activity activity, int curCameraId) {
        if (camera == null) {
            try {
                camera = Camera.open(curCameraId);
            } catch (Exception e) {
            }
        }
        Camera.CameraInfo info = new Camera.CameraInfo();

        Camera.getCameraInfo(curCameraId, info);
        WindowManager winManager = (WindowManager) activity.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);
    }

我使用 ExifInterface 解決了這個問題。 但是,如果有人可以在拍攝照片時檢測應用於圖像的默認角度,那仍然會很棒。

這是代碼:

public Bitmap decodeAndReturnBitmap(String filePath)
{
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds    = true;

    BitmapFactory.decodeFile(filePath, o);

    final int REQUIRED_SIZE = 512;

    int widthTemp = o.outWidth, heightTemp = o.outHeight;
    int scale     = 1;

    while (true) {
        if (widthTemp < REQUIRED_SIZE && heightTemp < REQUIRED_SIZE) {
            break;
        }

        widthTemp  /= 2;
        heightTemp /= 2;
        scale      *= 2;
    }

    ExifInterface exifInterface = null;

    try {
        exifInterface = new ExifInterface(filePath);
    } catch (IOException e) {
        e.printStackTrace();
    }

    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    Matrix matrix = new Matrix();

    switch(orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90 :
            matrix.setRotate(90.0f);
            break;

        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180.0f);
            break;

       case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(270.0f);
            break;

    }

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize          = scale;

    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM