开发者

Blackberry: how to flip a Bitmap upside down?

开发者 https://www.devze.com 2023-03-17 08:22 出处:网络
How to flip a Bitmap upside down? (I need this for loading an OpenGL texture in another program). Here is my failed try:

How to flip a Bitmap upside down?

(I need this for loading an OpenGL texture in another program).

Here is my failed try:

Blackberry: how to flip a Bitmap upside down?

stripe.png (courtesy of Pitr@OpenClipart):

Blackberry: how to flip a Bitmap upside down?

Flip.java:

import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import n开发者_运维知识库et.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;

public class Flip extends UiApplication {
    public static void main(String args[]) {
        Flip app = new Flip();
        app.enterEventDispatcher();
    }

    public Flip() {
        pushScreen(new MyScreen());
    }
} 

class MyScreen extends MainScreen {
    static final Bitmap STRIPE = flip(Bitmap.getBitmapResource("stripe.png"));

    public MyScreen() {
        setTitle("Flip the bitmap");
        add(new BitmapField(STRIPE));
        add(new ButtonField("Hello world"));
    }

    static Bitmap flip(Bitmap bitmap) {
        int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
        bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        for (int i = 0; i < bitmap.getHeight(); i++) {
            for (int j = 0; j < bitmap.getWidth(); j++) {
                int swap = argb[i * bitmap.getWidth() + j];
                argb[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
            }
        }
        bitmap.setARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        return bitmap;
    }
}


Try using this bit of code:

    for (int y = 0; y < bitmap.getHeight() / 2; y++) {
        int upper_row = bitmap.getWidth() * y;
        int lower_row = bitmap.getWidth() * (bitmap.getHeight() - 1 - y);
        for (int x = 0; x < bitmap.getWidth(); x++) {
            int temp = argb[upper_row + x];
            argb[upper_row + x] = argb[lower_row + x];
            argb[lower_row + x] = temp;
        }
    }


public Bitmap flip(Bitmap bitmap) {

    int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];

    int[] argb_flip = new int[bitmap.getWidth() * bitmap.getHeight()];

    bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
    for (int i = 0; i < bitmap.getHeight(); i++) {
        for (int j = 0; j < bitmap.getWidth(); j++) {
            int swap   = argb[i * bitmap.getWidth() + j];

            argb_flip[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
        }
    }

    bitmap.setARGB(argb_flip, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());

    return bitmap;
}

Try this, surely it will help you to flip the image by 180 degrees.

0

精彩评论

暂无评论...
验证码 换一张
取 消