繁体   English   中英

Android旋转位图中的像素,但位图变成白色/无

[英]Android rotate pixel in bitmap but the bitmap is turn to white/mising

我将指纹图像作为位图,然后使用以下代码旋转像素:

public Bitmap rotateImage(Bitmap rotateBmp)
{
        double radians=Math.toRadians(90);
        double cos, sin;
        cos=Math.cos(radians);
        sin=Math.sin(radians);
        boolean rotatePix[][]=new boolean[width][height];

        for(int i=0;i<width;++i)
        {
            for(int j=0;j<height;++j)
            {
                int centerX=core.x, centerY=core.y;
                int m=i - centerX;
                int n=j - centerY;
                int k=(int)(m * cos + n * sin);
                int l=(int)(n * cos - m * sin);

                k+=centerX;
                l+=centerY;



                if(!((k<0)||(k>width-1)||(k<0)||(k>height-1)))
                {
                    try
                    {
                        rotatePix[k][l]=binaryMap[i][j];
                    }
                    catch(Exception e)
                    {
                        Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        }

        for(int i=0;i<width;++i)
        {
            for(int j=0;j<height;++j)
            {
                if(rotatePix[i][j]==true)
                {
                    rotateBmp.setPixel(i, j, Color.BLACK);
                }
                else
                {
                    rotateBmp.setPixel(i, j, Color.WHITE);
                }
            }
        }

        return rotateBmp;
    //}
}

但是当我检查结果时,黑色像素变得比以前少了,我猜它变成了白色,因为当我在X和Y坐标中检查计算时,其中许多具有相同的X和Y新坐标,并且可能会改变黑色像素变为白色。 请告诉我如何旋转一个角度但颜色与以前相同的像素。 我将结果附在这里供您查看。 非常感谢您的帮助...

轮换结果

如果您只想旋转位图,则可以使用Matrix如下所示:

public Bitmap rotateBitmap (Bitmap rotateBmp) {
    int rotationDegree = 90;

    /* rotate the image based the rotation degree */
    Matrix matrix = new Matrix();
    matrix.postRotate(rotationDegree);

    rotateBmp = Bitmap.createBitmap(rotateBmp, 0, 0, rotateBmp.getWidth(),
            rotateBmp.getHeight(), matrix, true);

    // rotateBmp is now rotated by 90 degrees
    return rotateBmp;
}

我希望这有帮助。

暂无
暂无

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

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