【问题标题】:Rotating image in File for Android在 Android 文件中旋转图像
【发布时间】:2015-11-04 12:31:54
【问题描述】:

在裁剪图像之前尝试旋转图像时遇到问题。我尝试在要旋转的图像视图上调用setRotation,它成功地旋转了图像视图。我意识到这不是一个解决方案,因为当我调用 CropImageIntentBuilder 时,它会传入原始图像视图而没有任何旋转。

在下面的代码中,我尝试了一种不同的方法,将图像保存到文件并将该文件传递给 CropImageIntentBuilder,但我仍然遇到同样的问题。请让我知道您是否可以就此提供任何建议,或者我是否以错误的方式处理此问题。另外,如果我需要从应用中发布更多代码,请告诉我。

public static Bitmap rotateBitmap (Bitmap source, float angle){
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
        return  Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),matrix,true);
}


private void executeCropImageIntent() {
    //This is the cropIntent that is called using nexuss 10
    try{
         bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
        //mSelectedImage.setImageBitmap(rotateBitmap(bitmap, 90));
        rotateBitmap(bitmap, 90);
        try{

            FileOutputStream fOut = new FileOutputStream(mSingleton.mCropFileTemp);
            bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();}
        catch (Exception e) {
            e.printStackTrace();
            Log.i(null, "Save file error!");
    //      Toast.makeText(this, getResources().getString(R.string.messageMissingGPS), Toast.LENGTH_LONG).show();
        }

        }

    catch (IOException e) {
        Log.d("JudgeMainFragment", "cannot take picture", e);
    }

            CropImageIntentBuilder intentBuilder = new CropImageIntentBuilder(0, 0, 0, 0, Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
            //  intentBuilder.setSourceImage(Uri.parse(mData.getImage()));
            intentBuilder.setSourceImage(Uri.parse(mSingleton.mCropFileTemp.toURI().toString()));
            startActivityForResult(intentBuilder.getIntent(this), IJudgeSingleton.REQUEST_CODE_CROP_IMAGE);


    }

【问题讨论】:

    标签: android image file bitmap rotation


    【解决方案1】:

    您正在呼叫rotateBitmap(bitmap, 90);。但不将返回的BitMap 分配给任何变量。然后您使用的是未旋转的旧位图。所以改变这一行

    rotateBitmap(bitmap, 90);
    

    进入这个

    bitmap = rotateBitmap(bitmap, 90);
    

    【讨论】:

    • 你我的朋友这个答案太棒了,非常感谢它就像一个魅力。我已经为此苦苦挣扎了好几天,所以最后我决定继续前进,并在不到一天的时间内得到答案。我会赞成这个答案,但我的声誉还不足以做到这一点。再次感谢这真的很有帮助。
    • 是的,但我该怎么做呢?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    相关资源
    最近更新 更多