gpt4 book ai didi

java - 如何在android中旋转位图?

转载 作者:行者123 更新时间:2023-12-02 04:30:22 24 4
gpt4 key购买 nike

我的位图已旋转但与原始图像重叠。

位图源

public void onImageAvailable(ImageReader reader) {
final Image image = reader.acquireNextImage();
// final Image image = reader.acquireLatestImage();
final ByteBuffer yuvBytes = imageToByteBuffer(image);

// Convert YUV to RGB
final RenderScript rs = RenderScript.create(MainActivity.this);

Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
final Allocation allocationRgb = Allocation.createFromBitmap(rs, bitmap);

final Allocation allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);
allocationYuv.copyFrom(yuvBytes.array());

ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
scriptYuvToRgb.setInput(allocationYuv);
scriptYuvToRgb.forEach(allocationRgb);

allocationRgb.copyTo(bitmap);
int rotation = MainActivity.this.getWindowManager().getDefaultDisplay().getRotation();
int x = bitmap.getWidth();
int y = bitmap.getHeight();
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap, y, x, true);
Bitmap rotatedBitmap = rotateBitmap(bitmap1, getOrientation(rotation));

旋转位图函数

public Bitmap rotateBitmap(Bitmap original, float degrees) {
int width = original.getWidth();
int height = original.getHeight();

Matrix matrix = new Matrix();
matrix.preRotate(degrees);

Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, width, height, matrix, true);
Canvas canvas = new Canvas(rotatedBitmap);
canvas.drawBitmap(original, 5.0f, 0.0f, null);

return rotatedBitmap;
}

输出

原始位图堆叠在旋转位图之上。 enter image description here #问题

最佳答案

解决了

    public Bitmap rotateBitmap(Bitmap original, float degrees) {
int x = original.getWidth();
int y = original.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(degrees);
Bitmap rotatedBitmap = Bitmap.createBitmap(original , 0, 0, original .getWidth(), original .getHeight(), matrix, true);
return rotatedBitmap;
}

关于java - 如何在android中旋转位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56590783/

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