網上隨便找了個二維碼掃描工具,可是無論怎麼回事都掃不出來有些圖片,PM建議我把圖片偏白的地方改成純白,偏黑的地方改成純黑,於是方法如下:
private Bitmap makeBitmapBlackWhite(Bitmap baseBitmap) {
int width = baseBitmap.getWidth();
int height = baseBitmap.getHeight();
int color, r, g, b, a;
int[] oldPixels = new int[width * height];
int[] newPixels = new int[width * height];
Bitmap myCopyBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig());
baseBitmap2.getPixels(oldPixels, 0, width, 0, 0, width, height);
//然後就去遍歷這個數組oldPixels,並取出每個像素,然後再取出每個像素的ARGB屬性,然後通過想修改ARGB屬性,得到新的像素點
//並把新的像素點保存新的數組newPixels中
for (int i = 0; i < width * height; i++) {
color = oldPixels[i];//取出每一個像素點
if (color<0xffaaaaaa){
color=0xff000000;
}else{color=0xffffffff;}
newPixels[i] = color;
}
myCopyBitmap.setPixels(newPixels, 0, width, 0, 0, width, height);//畫好自己的圖片,然後保存方便查看
File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
Log.e("aaa","圖片路徑"+file.getAbsolutePath());
try {
FileOutputStream out2 = new FileOutputStream(file);
myCopyBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
return myCopyBitmap;
}
不出意外的話,會得到一個純黑純白的圖片。
燃鵝,還是掃不出來。
其實用下面這個框架就行了,上面我只是想記錄下怎麼改變圖片顏色而已(別打我,我是女生,嘻嘻)。
https://github.com/yipianfengye/android-zxingLibrary
這個掃描,良心產品啊,讓我度過了這次難關。具體使用,自己看嘍,關鍵是這裏邊選擇文件後可以查看到文件路徑,有些手機對文件路徑已經管理的很嚴格了,輕易不給看的。如果有其他需要的地方,也可以只看這段代碼。