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

标签 java android-bitmap

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

位图源

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/

相关文章:

Android 将图库中的图像复制到新文件中

java - 目标运行时 WildFly 在 maven 测试后解包

java - 如何在 Spring @Service 和非 Spring 对象之间集成?

java - 无法使用深度附件渲染到 FBO

如果返回零,Java 从文件中删除字符串

java - 捕获 GoogleJsonResponseException 但 e.getDetails() 返回 null

android - 特定 Activity 被销毁时释放内存

java - 每次我尝试更改油漆的属性(厚度或颜色)时,它都会更改 Canvas 上已有的现有油漆(android)(java)

android - 为什么 setImageBitmap 对自定义 ImageButton 没有影响?

java - Android:Canvas:尝试绘制太大