Android 尝试旋转位图但未成功

Android 尝试旋转位图但未成功,android,bitmap,xamarin,xamarin.android,Android,Bitmap,Xamarin,Xamarin.android,我正在编辑一个位图,以便为OCR扫描进行优化。我需要做的事情之一是将图像旋转270度。我正在使用以下代码: Matrix matrix = new Matrix(); matrix.PostRotate (270); canvas.DrawBitmap(alteredBitmap, matrix, paint); 显然,这对我不起作用。有人能指出我错在哪里吗? 位图来自一个字节[]这个方法对我来说一直很有效 Matrix matrix = new Matrix(); //myBitmap

我正在编辑一个位图,以便为OCR扫描进行优化。我需要做的事情之一是将图像旋转270度。我正在使用以下代码:

Matrix matrix = new Matrix();
matrix.PostRotate (270);
canvas.DrawBitmap(alteredBitmap, matrix, paint);
显然,这对我不起作用。有人能指出我错在哪里吗?
位图来自一个
字节[]

这个方法对我来说一直很有效

Matrix matrix = new Matrix();
   //myBitmap is the bitmap which is to be rotated
    matrix.postRotate(rotateDegree);
    Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), matrix, true);//Rotated Bitmap
如果这个位图是从url生成的,那么继续使用这个函数

public Bitmap decodeBitmap(File f) {

    try {
        //Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        //The new size we want to scale to
        final int REQUIRED_SIZE = 490;

        //Find the correct scale value. It should be the power of 2.
        int scale = 1;
        while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
            scale *= 2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {
    }
    return null;
}

这是我用来在项目中旋转
字节[]
的代码片段。它接收并返回一个
字节[]
,但通过删除最后5行代码,它将返回一个
位图
。它创造了奇迹:

public async Task<byte[]> RotateImage(byte[] source, int rotation)
{
    //This is optional, use it to reduce your images a little
    var options = new BitmapFactory.Options();
    options.InJustDecodeBounds = false;
    options.InSampleSize = 2;

    var bitmap = await BitmapFactory.DecodeByteArrayAsync(source, 0, source.Length, options);
    Matrix matrix = new Matrix();
    matrix.PostRotate(rotation);
    var rotated = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);

    var stream = new MemoryStream();
    await rotated.CompressAsync(Bitmap.CompressFormat.Jpeg, 100, stream);
    var result = stream.ToArray();

    await stream.FlushAsync();
    return result;
}
公共异步任务旋转图像(字节[]源,整数旋转)
{
//这是可选的,使用它可以稍微减少图像
var options=新的BitmapFactory.options();
options.InJustDecodeBounds=false;
options.InSampleSize=2;
var bitmap=await BitmapFactory.DecodeByteArrayAsync(source,0,source.Length,options);
矩阵=新矩阵();
矩阵。后旋转(旋转);
var rotated=Bitmap.CreateBitmap(位图,0,0,Bitmap.Width,Bitmap.Height,matrix,true);
var stream=newmemoryStream();
等待旋转的.CompressAsync(Bitmap.CompressFormat.Jpeg,100,流);
var result=stream.ToArray();
等待stream.FlushAsync();
返回结果;
}

所有可等待的调用都有非异步的对应调用,因此可以将其转换为以阻塞方式运行,而不会出现重大问题。请注意,删除
选项
变量可能会导致
OutOfMemoryException
,因此请确保在删除之前知道正在执行的操作。

定义“不起作用”。它会崩溃吗?它没有显示任何内容吗?它是否显示位图,但没有旋转?我没有使用xamarin,但我使用C#旋转了图像。在C#中,如果将图像旋转90/270,随着图像宽度的变化,我需要重新计算旋转图像的步幅degrees@Jason不工作=不旋转发动机image@wandering-你能提供一点代码吗?
current\u height=pixel\u arr\u rotated.GetLength(0);当前宽度=像素旋转。GetLength(1);步幅=当前宽度*每像素字节数;而(步幅%4!=0)步幅++此处pixel_arr_rotated是旋转原始矩阵后所有像素的2d数组或矩阵。跨距的计算如图所示问题是,如果我创建了另一个位图,我超出了内存您从文件中获取此位图,如果是,则需要正确解码以减小其大小,以避免内存泄漏扫描您提供的文档或少量代码?您正在调整其大小。我需要尽可能大的图片,这样带Tesseract的OCR就有最大的机会读取图片。你可以摆弄所需的大小变量,看看什么适合你的情况