我有项目,我从图库图片制作扫描Qr代码。它工作正常。但我的问题是我的应用程序只扫描了黑色和白色的qr。当我试图用蓝色和白色添加其他qr图像时,它无法读取。
这是我的代码
//call the onactivity result method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
//doing some uri parsing
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
//getting the image
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "File not found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
//decoding bitmap
Bitmap bMap = BitmapFactory.decodeStream(imageStream);
Scan.setImageURI(selectedImage);// To display selected image in image view
int[] intArray = new int[bMap.getWidth()*bMap.getHeight()];
//copy pixel data from the Bitmap into the 'intArray' array
bMap.getPixels(intArray, 0, bMap.getWidth(), 0, 0, bMap.getWidth(), bMap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Reader reader = new MultiFormatReader(); // use this otherwise
// ChecksumException
try {
Hashtable<DecodeHintType, Object> decodeHints = new Hashtable<DecodeHintType, Object>();
decodeHints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
final Result result = reader.decode(bitmap, decodeHints);
//*I have created a global string variable by the name of barcode to easily manipulate data across the application*//
barcode = result.getText().toString();
//do something with the results for demo i created a popup dialog
if(barcode!=null){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("" + barcode);
AlertDialog alert1 = builder.create();
alert1.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent (getBaseContext(),MainActivity.class);
startActivity(i);
}
});
alert1.setCanceledOnTouchOutside(false);
alert1.show();}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setIcon(R.mipmap.ic_launcher);
builder.setMessage("Nothing found try a different image or try again");
AlertDialog alert1 = builder.create();
alert1.setButton(DialogInterface.BUTTON_POSITIVE, "Done", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent (getBaseContext(),MainActivity.class);
startActivity(i);
}
});
alert1.setCanceledOnTouchOutside(false);
alert1.show();
}
//the end of do something with the button statement.
} catch (NotFoundException e) {
Toast.makeText(getApplicationContext(), "Nothing Found", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (ChecksumException e) {
Toast.makeText(getApplicationContext(), "Something weird happen, i was probably tired to solve this issue", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (FormatException e) {
Toast.makeText(getApplicationContext(), "Wrong Barcode/QR format", Toast.LENGTH_SHORT).show();
e.printStackTrace();
} catch (NullPointerException e) {
Toast.makeText(getApplicationContext(), "Something weird happen, i was probably tired to solve this issue", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
}