黑莓图像处理(草图效果和木炭效果)

时间:2012-06-04 10:03:15

标签: image-processing blackberry-eclipse-plugin

我是黑莓新手。 我正在尝试将普通图像转换为草图效果。我有代码在 ANDROID。中执行此操作 我试图在Blackberry中实现它但无法获得输出。这是android代码和我的黑莓代码。

这是android代码 -

public class ConvolutionMatrix
{
    public static final int SIZE = 3;

    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();
        Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

        int A, R, G, B;
        int sumR, sumG, sumB;
        int[][] pixels = new int[SIZE][SIZE];

        for(int y = 0; y < height - 2; ++y) {
            for(int x = 0; x < width - 2; ++x) {

                // get pixel matrix
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        pixels[i][j] = src.getPixel(x + i, y + j);
                    }
                }

                // get alpha of center pixel
                A = Color.alpha(pixels[1][1]);

                // 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) {
                        sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
                        sumB += (Color.blue(pixels[i][j]) * 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));
            }
        }

        // final image
        return result;
    }
}

以下是我尝试的Blackberry代码 -

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[] argb= new int[width*height];
        int[][]newargb=new int[width][height];
        src.getARGB(argb, 0, width, 0, 0, width, height);
        for(int y=0;y<=height;y++)
        {
            for (int x=0;x<=width;x++)
            {
                System.out.println(""+x);
                System.out.println(""+y);
                newargb[y][x]=argb[width*y+x];
            }
        }

        int[][] pixels = new int[SIZE][SIZE];

        for(int y = 0; y < height - 2; ++y) {

            for(int x = 0; x < width - 2; ++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];
                    }
                }

                A=pixels[1][1];
                sumR = sumG = sumB = 0;
                for(int i = 0; i < SIZE; ++i) {
                    for(int j = 0; j < SIZE; ++j) {
                        A =pixels[i][j] >> 24;
                        R =pixels[i][j]>> 16 & 0xFF;
                        G =pixels[i][j] >> 8 & 0xFF;
                        B =pixels[i][j] & 0xFF;

                        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; }

                for(int i = 0; i < SIZE; ++i) {

                    for(int j = 0; j < SIZE; ++j) {

                        pixels[i][j]=(A << 24) | (R << 16) | (G << 8) | B;
                        newargb[x ][ y]=pixels[i][j];
                    }
                }


            }
        }

        for(int y=0;y<=height;y++)
        {
            for (int x=0;x<=width;x++)
            {
                argb[width*y+x]=newargb[y][x];
            }
        }


        src.setARGB(argb, 0, width, 0, 0, width, height);
        return src;
    }
}

Android和Blackberry的通用代码 -

public Bitmap EmbossImage(Bitmap src) {
        System.out.println("In Emboss Effect Image method");

        double[][] SharpConfig = new double[][] {
                { 0 , -1, 0  },
                { -1, 5, -1 },
                { 0 , -1, 0  }
        };
        ConvolutionMatrix convMatrix = new ConvolutionMatrix(3);
        convMatrix.setAll(0);
        convMatrix.applyConfig(SharpConfig);
        convMatrix.Factor = 1;
            convMatrix.offset=130;
        return ConvolutionMatrix.computeConvolution3x3(src, convMatrix);
    }

1 个答案:

答案 0 :(得分:0)

我找到了自己问题的答案。我从这个网站找到了与卷积相关的代码: android image processing
请参阅此链接页面的评论部分。

这是android代码。只需通过更改Color类方法使其与黑莓兼容。