【问题标题】:Images saved from Camera preview on landscape从相机预览中保存的横向图像
【发布时间】:2019-02-02 06:11:28
【问题描述】:

我正在开发我的第一个 Android 应用。它使用来自 camera1 api 的相机预览(我知道它已被弃用,但我找不到易于理解的 camera2 资源)。

由于相机 API 不易于使用,因此该应用程序更难并且超出了我的水平。但我大部分时间都在那里。

我遇到的问题是我的相机预览总是以横向模式保存图片,无论方向如何。我查了一下,在实现 Exifinterfaces、矩阵和位图时遇到了很多麻烦。

这是我目前正在使用的代码。

public static File getPictureFile(int mediaType) {

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Camera App");
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("Camera App", "Failed to create directory");
            return null;
        }
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;

    if (mediaType == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

public void takePicturePreview() {
    Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getPictureFile(MEDIA_TYPE_IMAGE);
            if (pictureFile ==null) {
                Log.d(TAG, "Error creating media file, check storage permissions");
                return;
            }

            //ROTATION NEEDS TO OCCUR HERE
            //String filePath = pictureFile.getPath();
            //Bitmap bitmap = BitmapFactory.decodeFile(filePath);


            /*ExifInterface exif = null;
            try {
                exif = new ExifInterface(filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }*/

            //int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
            //Matrix matrix = new Matrix();
            //matrix.postRotate(orientation);

            //bitmap = Bitmap.createBitmap(bitmap, 0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                //bitmap.compress(Bitmap.CompressFormat.JPEG,85,fos);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    };



    mCamera.takePicture(null, null, mPicture);

}

cmets 中的所有内容都是我尝试使用但没有任何效果的测试代码。所以我尝试使用 BitmapFactory、Exifinterface 来获取方向,然后使用矩阵来旋转它,然后创建并保存位图,但它只是崩溃了。

在这一点上,整个exifinteraces,位图对我来说太不清楚了,真的很难理解发生了什么。

任何人都可以提供任何提示或帮助以允许我的应用以正确的方向保存照片吗?

【问题讨论】:

    标签: java android bitmap camera rotation


    【解决方案1】:

    如果使用旧的相机 API,您需要在调用 Camera.takePicture() 之前调用 Parameters.setRotation 和 Camera.setParameters()。

    文档包含有关如何获取当前设备方向的示例代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-04
      • 2011-04-19
      • 2019-04-13
      • 1970-01-01
      • 2018-06-17
      • 2015-06-24
      • 1970-01-01
      相关资源
      最近更新 更多