gpt4 book ai didi

android - 尝试旋转位图但没有成功

转载 作者:太空狗 更新时间:2023-10-29 14:51:04 25 4
gpt4 key购买 nike

我正在编辑位图以针对 OCR 扫描对其进行优化。我需要做的一件事是将图像旋转 270 度。我正在使用以下代码:

Matrix matrix = new Matrix();
matrix.PostRotate (270);
canvas.DrawBitmap(alteredBitmap, matrix, paint);

显然,这对我不起作用。有人可以指出我错在哪里吗?位图来自 byte[]

最佳答案

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

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

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

关于android - 尝试旋转位图但没有成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35256444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com