捕获的图像在nexus 9上旋转

时间:2015-02-03 12:27:33

标签: java android

我正在使用nexus 9和android L开发应用程序。我使用以下代码打开相机,拍照并将其存储在图库中。

        if(isCameraInUse())
            return;
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        mImageFile = getOutputFromCamera();
        if(mImageFile == null)
            return;
        Uri tempuri = Uri.fromFile(mImageFile);
        i.putExtra(MediaStore.EXTRA_OUTPUT, tempuri);
        startActivityForResult(i, CHOOSE_CAMERA_RESULT);

现在,当我在图像视图中显示捕获的图像时,它会旋转。然而,这只发生在nexus 9上。它不会发生在nexus5,7上。请帮助。

2 个答案:

答案 0 :(得分:1)

起初:

// GET IMAGEVIEW
ImageView photoFromCamera = (ImageView) rootView.findViewById(R.id.photo_from_camera);
// SET IMAGE
photoFromCamera.setImageBitmap(rotate(shrink(your_path, display_Width, display_height), getCameraPhotoOrientation(getActivity().getApplicationContext(), your_uri, your_path)));

第二名: 这3种方法将旋转,调整大小并获得方向。


public static Bitmap rotate(Bitmap b, int degrees) {
    if (degrees != 0 && b != null) {
        Matrix m = new Matrix();
        m.setRotate(degrees, (float) b.getWidth() / 2, (float) b.getHeight() / 2);

        try {
            Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), m, true);

            if (b != b2) {
                b.recycle();
                b = b2;
            }
        } catch (OutOfMemoryError ex) {
            throw ex;
        }
    }
    return b;
} 

public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath) {
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);

        File imageFile = new File(imagePath);
        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());

        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return rotate;
}

Bitmap shrinkmethod(String file,int width,int height){
    BitmapFactory.Options bitopt=new BitmapFactory.Options();
    bitopt.inJustDecodeBounds=true;
    Bitmap bit=BitmapFactory.decodeFile(file, bitopt);

    int h=(int) Math.ceil(bitopt.outHeight/(float)height);
    int w=(int) Math.ceil(bitopt.outWidth/(float)width);

    if(h>1 || w>1){
        if(h>w){
            bitopt.inSampleSize=h;
        }else{
            bitopt.inSampleSize=w;
        }
    }

    bitopt.inJustDecodeBounds=false;
    bit=BitmapFactory.decodeFile(file, bitopt);

    return bit;
}

答案 1 :(得分:0)

 final Matrix matrix = new Matrix();
                    final int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
                    int rotate = -1;

                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            rotate = 270;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            rotate = 180;
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            rotate = 90;
                            break;
                    }





                ivPreview.setVisibility(View.VISIBLE);
                Bitmap bitmap = rotateBitmap(imageBitmap, rotate);
                ivPreview.setImageBitmap(bitmap);

请在onActivityResult上尝试上面的代码,它可以帮助你