简体   繁体   English

在Android的File中旋转图像

[英]Rotating image in File for Android

I'm having problems with trying to rotate an image before cropping it. 裁剪之前尝试旋转图像时遇到问题。 I have tried calling setRotation on the Image View I want to rotate and it rotates the imageView successfully. 我尝试在要旋转的图像视图上调用setRotation ,它成功旋转了setRotation I realized this wasn't a solution because when I call CropImageIntentBuilder it passes in the original Image View without any rotation. 我意识到这不是解决方案,因为当我调用CropImageIntentBuilder时,它将在原始图像视图中传递而没有任何旋转。

In the code below I have tried a different approach by saving the image to a File and passing that file to CropImageIntentBuilder but I am still having the same problem. 在下面的代码中,我通过将图像保存到文件并将该文件传递给CropImageIntentBuilder尝试了一种不同的方法,但是我仍然遇到相同的问题。 Please let me know if there is any advice you can offer on this or if I'm approaching this the wrong way. 请让我知道您是否可以对此提供任何建议,或者如果我使用错误的方法。 Also if I need to post more of the code from the app please let me know. 另外,如果我需要从应用程序中发布更多代码,请告诉我。

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


    }

You are calling rotateBitmap(bitmap, 90); 您正在调用rotateBitmap(bitmap, 90); . but not assigning the returned BitMap to any variable. 但不会将返回的BitMap分配给任何变量。 and then you are using the old bitmap which is not rotated. 然后您正在使用未旋转的旧位图。 so change this line 所以改变这条线

rotateBitmap(bitmap, 90);

into this 进入这个

bitmap = rotateBitmap(bitmap, 90);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM