将浮雕和草图效果应用于Blackberry中的位图

时间:2012-04-05 05:53:42

标签: blackberry java-me

我有一个Blackberry项目,我想将普通图像更改为浮雕效果和草图效果。 我尝试了一个代码,但它给了我空指针异常...我使用了一个卷积矩阵类,并使用了它的方法。 有人可以帮我吗?

public class ToEmboss {

    public static Bitmap emboss(Bitmap src) {
        double[][] EmbossConfig = new double[][] {
            { -1 ,  0, -1 },
            {  0 ,  4,  0 },
            { -1 ,  0, -1 }
        };
        ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
        convMatrix.applyConfig(EmbossConfig);
        convMatrix.Factor = 1;
        convMatrix.Offset = 127;
        return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
    }
}


package mypackage;
import net.rim.device.api.system.Bitmap;

public class ConvolutionMatrix {

    public static final int SIZE = 3;
    public static int[][] pixel;
    public double[][] Matrix;
    public double Factor = 1;
    public double Offset = 1;

    public ConvolutionMatrix(int size) {
        Matrix = new double[size][size];
    }

    public void setAll(double value) {
        for (int x = 0; x < SIZE; ++x) {
            for (int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = value;
            } }}

    public void applyConfig(double[][] config) {
        for(int x = 0; x < SIZE; ++x) {
            for(int y = 0; y < SIZE; ++y) {
                Matrix[x][y] = config[x][y];
            }}}

    public static Bitmap computeConvolution3x3(Bitmap src, ConvolutionMatrix matrix) {
    int width = src.getWidth();
    int height = src.getHeight();
    int A, R, G, B;
    int sumR, sumG, sumB;
    int[][] pixels = new int[SIZE][SIZE];

    int [][] newargb=new int[width][height];

    int[] argb=new int[width*height];

    int a=0;
    src.getARGB(argb, 0, width, 0, 0, width, height);
    for(a=0;a<argb.length-1;a++)
    {
        A= argb[a] >> 24;
        R= argb[a] >> 16 & 0xFF;
        G= argb[a] >> 8 & 0xFF;
        B= argb[a] & 0xFF;
    }

    for(int i = 0; i <= height; ++i) {
        for(int j = 0; j <= width; ++j) {
            newargb[i][j]=argb[a];
            a++;
        }
    }

    for(int y = 0; y <= height; ++y) {
        for(int x = 0; x <= width; ++x) {
            // get pixel matrix
            for(int i = 0; i < SIZE; ++i) {
                for(int j = 0; j < SIZE; ++j) {
                    pixels[i][j] = newargb[x + i][ y + j];
                }
            }

            // get alpha of center pixel

            A = pixels[1][1]>>24;
            // init color sum
            sumR = sumG = sumB = 0;

            // get sum of RGB on matrix
            for(int i = 0; i < SIZE; ++i) {
                for(int j = 0; j < SIZE; ++j) {

                    A=pixels[i][j]>>24;
                    R=pixels[i][j]>>16 ;
                    G=pixels[i][j]>>8 ;
                    B=pixels[i][j] ;

                    sumR += (R * matrix.Matrix[i][j]);
                    sumG += (G * matrix.Matrix[i][j]);
                    sumB += (B * matrix.Matrix[i][j]);
                }
            }

            // get final Red
            R = (int)(sumR / matrix.Factor + matrix.Offset);
            if(R < 0) { R = 0; }
            else if(R > 255) { R = 255; }

            // get final Green
            G = (int)(sumG / matrix.Factor + matrix.Offset);
            if(G < 0) { G = 0; }
            else if(G > 255) { G = 255; }

            // get final Blue
            B = (int)(sumB / matrix.Factor + matrix.Offset);
            if(B < 0) { B = 0; }
            else if(B > 255) { B = 255; }

            // apply new pixel
            //result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));

            argb[x*width+y] = ((argb[x*width+y] & 0xff000000) | R<< 16 | G << 8 | B );
        }
    }
    src.setARGB(argb, 0, width, 0, 0, width, height);
    return src;
}
}

1 个答案:

答案 0 :(得分:0)

我看到public static int[][] pixel;并且在computeConvolution3x3中,有一个数组的读取,但看起来它没有被赋予任何东西,所以pixel将始终为null,这意味着第一次尝试从数组中读取将抛出NullPointerException