Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么从安卓系统拍摄照片后照片会旋转:硬件:摄像头api_Android_Image_Api_Camera_Rotation - Fatal编程技术网

Android 为什么从安卓系统拍摄照片后照片会旋转:硬件:摄像头api

Android 为什么从安卓系统拍摄照片后照片会旋转:硬件:摄像头api,android,image,api,camera,rotation,Android,Image,Api,Camera,Rotation,我正在使用android:hardware:CameraAPI来定制相机。我已经做了,使相机,但问题是,从相机拍照后,图片保存在旋转的形式。我使用的相机表面视图。 这是创建的曲面上的摄影机设置功能 try { File f = new File(imagePath); ExifInterface exif = new ExifInterface(f.getPath()); int orientation = exif.getAttributeInt(

我正在使用android:hardware:CameraAPI来定制相机。我已经做了,使相机,但问题是,从相机拍照后,图片保存在旋转的形式。我使用的相机表面视图。 这是创建的曲面上的摄影机设置功能

try {
    File f = new File(imagePath);
    ExifInterface exif = new ExifInterface(f.getPath());
    int orientation = exif.getAttributeInt(
            ExifInterface.TAG_ORIENTATION,
            ExifInterface.ORIENTATION_NORMAL);

    int angle = 0;

    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        angle = 180;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
        angle = 270;
    }

    Matrix mat = new Matrix();
    mat.postRotate(angle);
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;

    Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
            null, options);
    bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
            bmp.getHeight(), mat, true);
    ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100,
            outstudentstreamOutputStream);
    imageView.setImageBitmap(bitmap);

} catch (IOException e) {
    Log.w("TAG", "-- Error in setting image");
} catch (OutOfMemoryError oom) {
    Log.w("TAG", "-- OOM Error in setting image");
}

从相机捕获图像并旋转后尝试此操作。

您可以使用以下方法以实际方向显示/保存图像

public static Bitmap setImage(String filePath, Bitmap bitmap){
    File curFile = new File(filePath);
    Bitmap rotatedBitmap = null;

    try {
        ExifInterface exif = new ExifInterface(curFile.getPath());
        int rotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        int rotationInDegrees = exifToDegrees(rotation);
        Matrix matrix = new Matrix();
        if (rotation != 0.0f) {matrix.preRotate(rotationInDegrees);}
        rotatedBitmap = Bitmap.createBitmap(bitmap,0,0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);


    }catch(IOException ex){
        ex.printStackTrace();
    }
    return rotatedBitmap;
}

private static int exifToDegrees(int exifOrientation) {
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { return 90; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {  return 180; }
    else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {  return 270; }
    return 0;
}

图片旋转的原因是因为相机的方向。您可以读取图像exif数据以确定其方向。