2

在我的应用程序中,我通过相机捕获图像并将其路径保存在字符串变量(用于在服务器上发送图像的 SD 卡路径)中,那时我还在 ImageView 上设置了该图像。但是图像在横向中自动旋转,而不是按直角设置。我一直在 StackOverflow 和谷歌上搜索并找到 EXIF 轮换使用这个:

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

但它不工作。我的代码是:(裁剪后的操作代码是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();
}
4

1 回答 1

0

单击 或尝试使用此

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);

        }
    }
于 2012-11-08T11:36:20.420 回答