Java 从gallery扫描二维码,但只扫描黑白色的二维码
我有一个项目,我从画廊图像扫描二维码。它很好用。但我的问题是我的应用程序只扫描黑白色的qr。当我尝试添加其他带有蓝色和白色的qr图像时,它无法读取 这是我的密码Java 从gallery扫描二维码,但只扫描黑白色的二维码,java,android,android-studio,qr-code,zxing,Java,Android,Android Studio,Qr Code,Zxing,我有一个项目,我从画廊图像扫描二维码。它很好用。但我的问题是我的应用程序只扫描黑白色的qr。当我尝试添加其他带有蓝色和白色的qr图像时,它无法读取 这是我的密码 //call the onactivity result method @Override protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(r
//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();
}
}
}
}
//调用onactivity结果方法
@凌驾
ActivityResult上受保护的void(int-requestCode、int-resultCode、Intent-ImageReturnedContent){
super.onActivityResult(请求代码、结果代码、图像返回内容);
开关(请求代码){
案例选择图片:
if(resultCode==RESULT\u OK){
//做一些uri解析
Uri selectedImage=imageReturnedIntent.getData();
InputStream imageStream=null;
试一试{
//获取图像
imageStream=getContentResolver().openInputStream(SelecteImage);
}catch(filenotfounde异常){
Toast.makeText(getApplicationContext(),“找不到文件”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}
//解码位图
位图bMap=BitmapFactory.decodeStream(imageStream);
Scan.setImageURI(selecteImage);//在图像视图中显示所选图像
int[]intArray=new int[bMap.getWidth()*bMap.getHeight()];
//将位图中的像素数据复制到“intArray”数组中
bMap.getPixels(intArray,0,bMap.getWidth(),0,0,bMap.getWidth(),bMap.getHeight());
RGBLuminanceSource=新的RGBLuminanceSource(bMap.getWidth(),bMap.getHeight(),intArray);
BinaryBitmap位图=新的BinaryBitmap(新的混合二进制程序(源));
Reader Reader=new multiformatrader();//否则请使用此选项
//checksum异常
试一试{
Hashtable decodeHits=新的Hashtable();
DecodeHintType.TRY_,Boolean.TRUE);
decodeHints.put(DecodeHintType.PURE_条形码,布尔值.TRUE);
最终结果=reader.decode(位图、解码提示);
//*我创建了一个名为barcode的全局字符串变量,以便在整个应用程序中轻松操作数据*//
条形码=result.getText().toString();
//对演示结果做些什么我创建了一个弹出对话框
如果(条形码!=null){
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“扫描结果”);
builder.setIcon(R.mipmap.ic_启动器);
builder.setMessage(“+”条形码);
AlertDialog alert1=builder.create();
alert1.setButton(DialogInterface.BUTTON_正值,“完成”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
Intent i=新的Intent(getBaseContext(),MainActivity.class);
星触觉(i);
}
});
alert1.SetCanceledOnTouchOut(假);
alert1.show();}
其他的
{
AlertDialog.Builder=新建AlertDialog.Builder(此);
builder.setTitle(“扫描结果”);
builder.setIcon(R.mipmap.ic_启动器);
setMessage(“未找到任何内容,请尝试其他图像或重试”);
AlertDialog alert1=builder.create();
alert1.setButton(DialogInterface.BUTTON_正值,“完成”,新建DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
Intent i=新的Intent(getBaseContext(),MainActivity.class);
星触觉(i);
}
});
alert1.SetCanceledOnTouchOut(假);
alert1.show();
}
//按钮语句的结尾。
}捕获(未发现异常){
Toast.makeText(getApplicationContext(),“找不到”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}捕获(检查异常){
Toast.makeText(getApplicationContext(),“发生了一些奇怪的事情,我可能已经厌倦了解决这个问题”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}捕获(格式化异常){
Toast.makeText(getApplicationContext(),“错误的条形码/QR格式”,Toast.LENGTH\u SHORT.show();
e、 printStackTrace();
}捕获(NullPointerException e){
Toast.makeText(getApplicationContext(),“发生了一些奇怪的事情,我可能已经厌倦了解决这个问题”,Toast.LENGTH_SHORT.show();
e、 printStackTrace();
}
}
}
}
你有解决方案吗?@sanjeev还没有..你有解决方案吗?@sanjeev还没有。。