1
我有一個黑莓項目,我希望將普通圖像更改爲浮雕效果和草圖效果。 我試過了一個代碼,但它給了我空指針異常...我使用了一個卷積矩陣類,並且使用了它的方法。 任何人都可以幫助我嗎?對黑莓中的位圖應用浮雕和素描效果
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;
}
}
在這段代碼中應該做些什麼來獲得浮雕圖像效果? – 2012-04-09 05:12:05
如果你有浮雕效果的代碼,請與我分享我非常需要它嗎? – 2012-04-09 05:22:28
我已經更新了我的代碼。現在它給了我數組索引出界的錯誤。爲什麼會這樣?查看我的更新代碼並請告訴我我哪裏出錯了? – 2012-04-09 09:43:02