在Android中的少数设备上使用自定义相机实现捕获的照片方向不正确

时间:2016-07-20 06:38:36

标签: android-camera surfaceview screen-orientation android-bitmap

我已经实现了自定义相机来拍照。在少数设备上,图片未按照拍摄方向显示。它在三星T350 8英寸平板电脑上旋转180度。但它在我测试的所有其他设备上按预期工作。

public class TouchTrack {

    private static final int INVALID = -1;
    private static final int POINTERMAX = 5;

    HashMap<Integer, TouchData> touches = new HashMap<>();
    ArrayList<TouchData> fingers = new ArrayList<>(POINTERMAX);


    public void setEventPointers(MotionEvent event) {
        int count = event.getPointerCount();
        count = Math.min(count, POINTERMAX);
        for (int i = 0; i < count; i++) {
            int id = event.getPointerId(i);
            TouchData td = touches.get(id);
            if (td == null) {
                //Log.e("Touch", "Log does not have records for ID: " + id);
                return;
            }
            td.x = event.getX(i);
            td.y = event.getY(i);
        }
    }

    public void setLostPointer(MotionEvent event) {
        int index = event.getActionIndex();
        if (index >= POINTERMAX) return;
        int id = event.getPointerId(index);
        TouchData finger = touches.get(id);
        finger.id = INVALID;
    }

    public void setNewPointer(MotionEvent event) {
        int index = event.getActionIndex();
        if (index >= POINTERMAX) return;
        int id = event.getPointerId(index);

        for (int i = 0; i < POINTERMAX; i++) {
            TouchData finger = fingers.get(i);
            if (finger.id == INVALID) {
                finger.id = id;
                finger.x = event.getX();
                finger.y = event.getY();
                touches.put(id, finger);
                break;
            }
        }
    }

    public double lastEventX(int requestIndex) {
        if ((requestIndex >= 0) && (requestIndex < POINTERMAX)) {
            TouchData td = fingers.get(requestIndex);
            if (td == null) return 0;
            return td.x;
        }
        return 0;
    }

    public double lastEventY(int requestIndex) {
        if ((requestIndex >= 0) && (requestIndex < POINTERMAX)) {
            TouchData td = fingers.get(requestIndex);
            if (td == null) return 0;
            return td.y;
        }
        return 0;
    }

    public boolean isPointerActive(int requestIndex) {
        if ((requestIndex >= 0) && (requestIndex < POINTERMAX)) {
            TouchData td = fingers.get(requestIndex);
            if (td == null) return false;
            return td.id != INVALID;
        }
        return false;
    }

    public void start(float x, float y) {
        touches.clear();
        fingers.clear();
        for (int i = 0; i < POINTERMAX; i++) {
            fingers.add(new TouchData(x, y));
        }
    }

    public void stop() {
        touches.clear();
        fingers.clear();
    }

    public class TouchData {
        static final int INVALID = -1;
        public int id = INVALID;
        public double x = 0;
        public double y = 0;

        public TouchData(double x, double y) {
            this.x = x;
            this.y = y;
        }
    }
}

这是回调:

public int setCameraDisplayOrientation(Activity activity, int mCameraId, Camera mCamera, boolean portrait) {

    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(mCameraId, 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;
    }
    mCamera.setDisplayOrientation(result);
    return result;
}

请帮帮我。

0 个答案:

没有答案