是否可以在android中将float转换为矩阵,将矩阵转换为位图?

时间:2017-01-18 08:47:26

标签: android matrix bitmap android-camera

我已经将onPreviewFrame中的数据转换为rgb,这是一个int数组。高斯模糊使用float / double值,我无法找到将float / double值转换为位图的方法。

我想到的唯一方法是将这些浮点值转换为Matrix,然后转换为位图。但我不确定这是否可能。

public Bitmap getProcessedImage(byte[] image){
    Bitmap bmp = null;
    int rgb[] = new int[image.length];

    decodeYUV420SP(rgb, image);
    convertToGrayscale(rgb);
    applyGaussianBlur(rgb);

    bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmp.copyPixelsFromBuffer(IntBuffer.wrap(rgb));

    return bmp;
}

public void convertToGrayscale(int rgb[]){
    for(int i = 0 ; i < rgb.length;i++){
        int R = (rgb[i] >> 16) & 0xff;
        int G = (rgb[i] >> 8) & 0xff;
        int B = rgb[i] & 0xff;
        int gray = (R + G + B )/ 3 ;
        rgb[i] = 0xFF000000 | (gray << 16) | (gray << 8) | gray;
    }
}

//Method from Ketai project!
void decodeYUV420SP(int[] rgb, byte[] yuv420sp) {

    final int frameSize = width * height;

    for (int j = 0, yp = 0; j < height; j++) {       int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
        for (int i = 0; i < width; i++, yp++) {
            int y = (0xff & ((int) yuv420sp[yp])) - 16;
            if (y < 0)
                y = 0;
            if ((i & 1) == 0) {
                v = (0xff & yuv420sp[uvp++]) - 128;
                u = (0xff & yuv420sp[uvp++]) - 128;
            }

            int y1192 = 1192 * y;
            int r = (y1192 + 1634 * v);
            int g = (y1192 - 833 * v - 400 * u);
            int b = (y1192 + 2066 * u);

            if (r < 0)r = 0;else if (r > 262143)r = 262143;
            if (g < 0)g = 0;else if (g > 262143)g = 262143;
            if (b < 0)b = 0;else if (b > 262143)b = 262143;

            rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
        }
    }
}

public void applyGaussianBlur(int rgb[]){
    int kernelWidth = 3, kernelHeight = 3;
    float darray[][] = new float[width][height];

    float matrix[][] = new float[kernelWidth][kernelHeight];
    float sigma = 3.0f;
    float sum = 0;


    for(int j = 0, ct = 0; j < width;j++){
        for(int m = 0; m < height;m++, ct++){
            darray[j][m] = rgb[ct];
        }
    }

    for(int x = -1; x < kernelWidth-1;x++){
        for(int y = -1; y < kernelHeight-1; y++){
            matrix[x+1][y+1] = (float)((1/(2*Math.PI*Math.pow(sigma, 2))) * Math.exp(-(Math.pow(x, 2)+Math.pow(y, 2))/(2*Math.pow(sigma, 2))));
            sum+=matrix[x+1][y+1];
        }
    }

    for(int x = -1; x < kernelWidth-1;x++){
        for (int y = -1; y < kernelHeight-1; y++) {
            matrix[x+1][y+1] = matrix[x+1][y+1]/sum;
        }
    }


    for (int j = 1; j < width-1; j++){
        for (int m = 1; m < height-1;m++){
            darray[j-1][m-1] = (darray[j-1][m-1] * matrix[0][0]);
            darray[j-1][m] = (darray[j-1][m] * matrix[0][1]);
            darray[j-1][m+1] = (darray[j-1][m+1] * matrix[0][2]);

            darray[j][m-1] = (darray[j][m-1] * matrix[1][0]);
            darray[j][m] = (darray[j][m] * matrix[1][1]);
            darray[j][m+1] = (darray[j][m+1] * matrix[1][2]);

            darray[j+1][m-1] = (darray[j+1][m-1] * matrix[2][0]);
            darray[j+1][m] = (darray[j+1][m] * matrix[2][1]);
            darray[j+1][m+1] = (darray[j+1][m+1] * matrix[2][2]);
        }
    }

    for(int j = 0, ct = 0; j < width;j++){
        for(int m = 0; m < height;m++, ct++){
            rgb[ct] = Math.round(darray[j][m]);
        }
    }
}

0 个答案:

没有答案