BlackBerry Bitmap Rotation - PullRequest
       7

BlackBerry Bitmap Rotation

0 голосов
/ 15 марта 2012

Я попытался повернуть растровое изображение в BlackBerry двумя способами

1 -

public static Bitmap rotateImage(Bitmap oldB, int angle) {
    int w = oldB.getWidth();
    int h = oldB.getHeight();
    double angRad = (angle % 360) * (Math.PI / 180);
    Bitmap newB = new Bitmap(w, h);
    int[] oldD = new int[w * h];
    int[] newD = new int[w * h];
    oldB.getARGB(oldD, 0, w, 0, 0, w, h);

    int axisX = w / 2;
    int axisY = h / 2;

    for (int x = 0; x < oldD.length; x++) {
        int oldX = x % w;
        int oldY = x / w;
        int op = oldX - axisX;
        int adj = oldY - axisY;
        double oldT = MathUtilities.atan2(op, adj);
        double rad = Math.sqrt((op * op) + (adj * adj));
        double newT = oldT + angRad;
        int newX = (int) MathUtilities.round((rad * Math.sin(newT))
                + (double) axisX);
        int newY = (int) MathUtilities.round((rad * Math.cos(newT))
                + (double) axisY);
        if (newX < 0 || newY < 0 || newX >= w || newY >= h) {
            newD[x] = 0x00000000;
        } else {
            newD[x] = oldD[(newY * w) + newX];
        }
    }

    newB.setARGB(newD, 0, w, 0, 0, w, h);
    return newB;
}

2 - второй способ с использованием drawTexturedPath

------function

private void drawRotatedBitmap(Graphics graphics, Bitmap bm, int angle,
        int x, int y) {
    int w = bm.getWidth();
    int h = bm.getHeight();
    double a = Math.toRadians(angle);
    int x1 = (int) (x - h * Math.sin(a));
    int y1 = (int) (y + h * Math.cos(a));
    int x2 = (int) (x1 + w * Math.cos(a));
    int y2 = (int) (y1 + w * Math.sin(a));
    int x3 = (int) (x + w * Math.cos(a));
    int y3 = (int) (y + w * Math.sin(a));
    int xPts[] = { x, x1, x2, x3 };
    int yPts[] = { y, y1, y2, y3 };
    int fAngle = Fixed32.toFP(angle);
    int dvx = Fixed32.cosd(fAngle);
    int dux = -Fixed32.sind(fAngle);
    int dvy = Fixed32.sind(fAngle);
    int duy = Fixed32.cosd(fAngle);

    graphics.drawTexturedPath(xPts, yPts, null, null, 0, 0, dux, dvx, duy,
            dvy, bm);

}

------ Как вызвать

Graphics graphics = Graphics.create(circleBmp);
drawRotatedBitmap(graphics, , 45, 0, 0);
circleBitmapField.setBitmap(circleBmp);

Первый способ слишком медленный, а второй - нарисовать растровое изображение в неправильном положении

Может ли кто-нибудь помочь мне настроить любой из них?или есть другой способ быстрого и точного поворота растрового изображения.

Спасибо за помощь .....

1 Ответ

1 голос
/ 15 марта 2012

Вам нужно использовать ImageManipulator класс. Найдите здесь документ «Как».

...