我有两个Android设备。 CameraSource对它们的工作方式有所不同。
对于一台设备,照片的方向正确,保存后不会旋转。但是对于其他设备,以错误的方向保存了照片。

我创建了CameraSource:

source = new CameraSource.Builder(context, detector) .setRequestedPreviewSize(640, 480) .setFacing(cameraId) .setRequestedFps(30.0f) .setAutoFocusEnabled(true) .build();

我创建了带有操作的按钮:

source.takePicture(null, new CameraSource.PictureCallback() { @Override public void onPictureTaken(byte[] bytes) { File folder = PhotoUtils.getGalleryFolder(); writeFileIntoDevice(bytes, folder.getAbsolutePath()); } });

private String writeFileIntoDevice(byte[] data, String path) {
    Bitmap orignalImage = BitmapFactory.decodeByteArray(data, 0, data.length);

    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_hhmmss");
    String fileName = formatter.format(new Date());

    File file = new File(path, fileName + ".jpg");

    try (FileOutputStream stream = new FileOutputStream(file)) {
        orignalImage.compress(Bitmap.CompressFormat.JPEG, 80, stream);
        Log.i(PhotoCreator.class.getName(), "photo was saved to " + path);
    } catch (Exception e) {
        Log.e(PhotoCreator.class.getName(), "can't save photo", e);
    }

    return file.getAbsolutePath();
}


设备上的照片方向不同,保存后我尝试旋转位图,但未成功。

我尝试了这个:Controlling the camera to take pictures in portrait doesn't rotate the final images

而这:Android camera resulted image should be rotated after the capture?

如何为所有设备正确旋转图像?

最佳答案

使用两种不同的设备,我得到两种不同的结果。我刚刚启动相机。使用一台设备,我可以正确地获得视频预览,而使用另一台设备,我可以得到顺时针旋转90度的视频预览。我的应用在Android平板电脑上横向显示。

我没有找到解决此相机旋转问题的任何解决方案。

    cameraView = (SurfaceView)findViewById(R.id.cameraView);

    barcodeDetector = new BarcodeDetector.Builder(this)
            .setBarcodeFormats(Barcode.QR_CODE)
            .build();

    cameraSource = new CameraSource
            .Builder(this, barcodeDetector)
            .setAutoFocusEnabled(true)
            .setRequestedFps(60)
            .setRequestedPreviewSize(640, 480)
            .setFacing(CameraSource.CAMERA_FACING_FRONT)
            .build();

    cameraSource.start(holder);

关于android - Android CameraSource在多个设备上的工作方式不同,并且不会旋转图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36390431/

10-11 22:23
查看更多