Android图像捕获方向错误(自定义相机)

时间:2015-04-13 14:39:20

标签: android camera

我一直在努力解决下面的问题,我找到了一些教程,并制作了一个自定义相机,SurfaceView作为预览版。 我主要关心前置摄像头。 现在,当我在预览工作正常时拍摄图像时,图像将在SD卡中以错误的旋转方式保存。

以下是两个主要功能。设置相机和拍照。

*private void setUpCamera(Camera c) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        rotation = getWindowManager().getDefaultDisplay().getRotation();
        int degree = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degree = 0;
                break;
            case Surface.ROTATION_90:
                degree = 90;
                break;
            case Surface.ROTATION_180:
                degree = 180;
                break;
            case Surface.ROTATION_270:
                degree = 270;
                break;

            default:
                break;
        }

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            // frontFacing
            rotation = (info.orientation + degree) % 360;
            rotation = (360 - rotation) % 360;
        } else {
            // Back-facing
            rotation = (info.orientation - degree + 360) % 360;
        }

        c.setDisplayOrientation(rotation);
        Parameters params = c.getParameters();

        List<String> focusModes = params.getSupportedFlashModes();
        if (focusModes != null) {
            if (focusModes
                    .contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
                params.setFlashMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            }
        }

        params.setRotation(rotation);
        c.setParameters(params);
    }*


private void takeImage() {
        camera.takePicture(null, null, new PictureCallback() {

            private File imageFile;

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                try {
                    // convert byte array into bitmap
                    Bitmap loadedImage = null;

                    loadedImage = BitmapFactory.decodeByteArray(data, 0,data.length);

                    // rotate Image
                    Matrix rotateMatrix = new Matrix();
                    rotateMatrix.postRotate(rotation);
                    loadedImage = Bitmap.createBitmap(loadedImage, 0, 0,loadedImage.getWidth(), loadedImage.getHeight(),rotateMatrix, false);

                    //create folder if it doesnt exists
                    String state = Environment.getExternalStorageState();
                    File folder = null;
                    if (state.contains(Environment.MEDIA_MOUNTED)) {
                        folder = new File(Environment
                                .getExternalStorageDirectory() + "/Squote");
                    } else {
                        folder = new File(Environment
                                .getExternalStorageDirectory() + "/Squote");
                    }
                    boolean success = true;
                    if (!folder.exists()) {
                        success = folder.mkdirs();
                    }
                    if (success) {
                        // Create a media file name
                        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                                Locale.getDefault()).format(new Date());
                        imageFile = new File(folder.getAbsolutePath()
                                + File.separator
                                + "IMG_"
                                + timeStamp
                                + ".jpg");

                        imageFile.createNewFile();
                    } else {
                        Toast.makeText(getBaseContext(), "Image Not saved",Toast.LENGTH_SHORT).show();
                        return;
                    }

                    ByteArrayOutputStream ostream = new ByteArrayOutputStream();

                    // save image into gallery
                    loadedImage.compress(CompressFormat.JPEG, 100, ostream);

                    FileOutputStream fout = new FileOutputStream(imageFile);
                    fout.write(ostream.toByteArray());
                    fout.close();
                    ContentValues values = new ContentValues();

                    values.put(Images.Media.DATE_TAKEN,System.currentTimeMillis());
                    values.put(Images.Media.MIME_TYPE, "image/jpeg");
                    values.put(MediaStore.MediaColumns.DATA, imageFile.getAbsolutePath());

                    //release camera and return image
                    releaseCamera();
                    Intent intentMessage = new Intent();
                    intentMessage.putExtra("imageUri", imageFile.getAbsolutePath());
                    setResult(RESULT_OK, intentMessage);
                    finish();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }

在代码的rotate image部分,如果我放270它显示正确但我在那里有另一个问题。图像与预览不同,但它是镜像的。我认为固定的270值也不是解决方案,因为它会弄乱后置摄像头或人像照片。

非常感谢任何帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

这不是问题,这是前置摄像头的工作原理。即使您使用手机的相机应用程序拍照,也会在保存时翻转。如果这是您尝试获得的结果,则可以实现垂直翻转图像的方法。