Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.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 为什么相机拍摄时图像会旋转?_Android - Fatal编程技术网

Android 为什么相机拍摄时图像会旋转?

Android 为什么相机拍摄时图像会旋转?,android,Android,在我的应用程序中,我用相机捕捉图像,并将其路径保存在字符串变量SD卡路径中,以便在服务器上发送图像,同时我还在ImageView上设置该图像。但图像会在横向自动旋转,而不是按直角设置。我一直在StackOverflow和google上搜索并找到EXIF rotation,请使用以下内容: 但它不起作用。我的代码是: 交叉后操作代码为onActivityResult case AppConstants.CROP_FROM_CAMERA: if (data != null) {

在我的应用程序中,我用相机捕捉图像,并将其路径保存在字符串变量SD卡路径中,以便在服务器上发送图像,同时我还在ImageView上设置该图像。但图像会在横向自动旋转,而不是按直角设置。我一直在StackOverflow和google上搜索并找到EXIF rotation,请使用以下内容:

但它不起作用。我的代码是: 交叉后操作代码为onActivityResult

case AppConstants.CROP_FROM_CAMERA:
        if (data != null) {
            Bundle extras = data.getExtras();

            if (extras != null) {
                Bitmap photo = extras.getParcelable("data");
                File file = new File("/sdcard/bidnear/");
                if (!file.isDirectory())
                    file.mkdir();
                imageUrl = "/sdcard/bidnear/thumbimgcrop.png";
                file = new File("/sdcard/bidnear/thumbimgcrop.png");
                try {
                    photo = rotateImage(photo,mImageCaptureUri);
                    photo.compress(Bitmap.CompressFormat.PNG, 100,
                            new FileOutputStream(file));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                objimg.setBackgroundResource(0);
                objimg.setImageBitmap(photo);

            }
        }
旋转方法为:

private Bitmap rotateImage(Bitmap objbitmap,Uri uri)
{
    Matrix matrix = new Matrix();
    float rotation =rotationForImage(MyProfile.this, uri);
    if (rotation != 0f) {
         matrix.preRotate(rotation);
    }

    Bitmap resizedBitmap = Bitmap.createBitmap(
            objbitmap, 0, 0,80,80, matrix, true);
    return resizedBitmap;
}
它不起作用;我打开相机和拍摄的代码是:

 private void setUserImage() {

    final String[] objimagechooseoptions = new String[] {
            AppConstants.SELECT_CAMERA, AppConstants.SELECT_GALLERY };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.select_dialog_item, objimagechooseoptions);
    AlertDialog.Builder objbuilder = new AlertDialog.Builder(this);
    objbuilder.setTitle("Select Image");

    objbuilder.setAdapter(adapter, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int item) { // pick from
                                                                // camera
            if (item == 0) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                mImageCaptureUri = Uri.fromFile(new File(Environment
                        .getExternalStorageDirectory(), "tmp_avatar_"
                        + String.valueOf(System.currentTimeMillis())
                        + ".png"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                        mImageCaptureUri);
                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent,
                            AppConstants.PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
            } else { // pick from file
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Complete action using"),
                        AppConstants.PICK_FROM_FILE);
            }
        }
    });
    final AlertDialog dialog = objbuilder.create();
    dialog.show();
}
单击或尝试此选项

case AppConstants.CROP_FROM_CAMERA:
    if (data != null) {
        Bundle extras = data.getExtras();

        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            File file = new File("/sdcard/bidnear/");
            if (!file.isDirectory())
                file.mkdir();
            imageUrl = "/sdcard/bidnear/thumbimgcrop.png";
            file = new File("/sdcard/bidnear/thumbimgcrop.png");


    try {

    ExifInterface exif = new ExifInterface(filePath);
    orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
    //Toast.makeText(getApplicationContext(), ""+orientation, 1).show();
    Log.v("log", "ort is "+orientation);

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


    if (orientation==6)
        {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(90);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }
                else if (orientation==8)
                {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(270);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }

                else if (orientation==3)
                {
                    Matrix matrix = new Matrix();
                    matrix.postRotate(180);
                    photo = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
                }
                Log.v("log", "before width is "+photo.getWidth() + " and height is "+photo.getHeight());


            objimg.setBackgroundResource(0);
            objimg.setImageBitmap(photo);

        }
    }