This article is a mirror article of machine translation, please click here to jump to the original article.

View: 11144|Reply: 2

About the problem of Chinese garbled characters when using ZXing to scan QR codes

[Copy link]
Posted on 3/8/2015 11:36:08 PM | | |
The function of the recent project requires the use of scanning QR codes. I refer to Google's open source project ZXing: After the function is completed: I found that there was no problem in scanning the barcode: But when scanning the QR code, some of them are garbled: Or not a Chinese question: I referred to a lot of information on the Internet. But the effect is still not ideal: Later, it was found that as long as it is encoded in the format of ISO-8859-1, the result is taken out and then converted accordingly: The problem is solved. You don't need to modify the source code: here's how to do it

First: First set its default encoding in the CaptureActivity class


  1. decodeFormats = null;
  2. characterSet = "ISO-8859-1";
Copy code


Second: You convert the corresponding encoding in the activity that retrieves the result. Here I am working on it in the ResultActivity class


  1. public class ResultActivity extends Activity implements OnClickListener{

  2.         private TextView dec_result;
  3.         private String resultStr;
  4.         private ImageView codeImage;
  5.         private Button returnChoujiang,returnJubao;
  6.         @Override
  7.         protected void onCreate(Bundle savedInstanceState) {
  8.                 // TODO Auto-generated method stub
  9.                 super.onCreate(savedInstanceState);
  10.                 setContentView(R.layout.dec_result);
  11.                 init();
  12.                 dec_result=(TextView)findViewById(R.id.dec_result);
  13.                 codeImage=(ImageView)findViewById(R.id.code_image);
  14.                 Intent intent=getIntent();
  15.                 resultStr=intent.getStringExtra("result");
  16.                 String UTF_Str="";
  17.                 String GB_Str="";
  18.                 boolean is_cN=false;
  19.                 try {
  20.                         System.out.println("------------"+resultStr);
  21.                         UTF_Str=new String(resultStr.getBytes("ISO-8859-1"),"UTF-8");
  22.                         System.out.println("这是转了UTF-8的"+UTF_Str);
  23.                         is_cN=IsChineseOrNot.isChineseCharacter(UTF_Str);
  24.                         //防止有人特意使用乱码来生成二维码来判断的情况
  25.                         boolean b=IsChineseOrNot.isSpecialCharacter(resultStr);
  26.                         if(b){
  27.                                 is_cN=true;
  28.                         }
  29.                         System.out.println("是为:"+is_cN);
  30.                         if(!is_cN){
  31.                                 GB_Str=new String(resultStr.getBytes("ISO-8859-1"),"GB2312");
  32.                                 System.out.println("这是转了GB2312的"+GB_Str);
  33.                         }
  34.                 } catch (UnsupportedEncodingException e) {
  35.                         // TODO Auto-generated catch block
  36.                         e.printStackTrace();
  37.                 }
  38.                
  39.                
  40.                 Bitmap bm=intent.getParcelableExtra("IMG_return");
  41.                 if(is_cN){
  42.                         dec_result.setText(UTF_Str);
  43.                 }else{
  44.                         dec_result.setText(GB_Str);
  45.                 }
  46.                 codeImage.setImageBitmap(bm);
  47.                
  48.                 returnChoujiang.setOnClickListener(this);
  49.                 returnJubao.setOnClickListener(this);
  50.         }
Copy code


Third: The following is to judge whether it is in Unicode encoding


  1. public class IsChineseOrNot {
  2.        
  3.     public static final boolean isChineseCharacter(String chineseStr) {  
  4.         char[] charArray = chineseStr.toCharArray();  
  5.         for (int i = 0; i < charArray.length; i++) {         
  6.                 //是否是Unicode编码,除了"&#65533;"这个字符.这个字符要另外处理
  7.             if ((charArray[i] >= '\u0000' && charArray[i] < '\uFFFD')||((charArray[i] > '\uFFFD' && charArray[i] < '\uFFFF'))) {  
  8.                 continue;
  9.             }
  10.             else{
  11.                     return false;
  12.             }
  13.         }  
  14.         return true;  
  15.     }  
  16.    
  17.     public static final boolean isSpecialCharacter(String str){
  18.             //是"&#65533;"这个特殊字符的乱码情况
  19.             if(str.contains("&#239;&#191;&#189;")){
  20.                     return true;
  21.             }
  22.             return false;
  23.     }
  24. }
Copy code


Finally, this solves the Chinese garbled problem of ZXing




Previous:PHP generates a QR code example
Next:What are the effects of 360 Website Guard on website optimization?
Posted on 9/4/2015 1:20:39 PM |
It's so powerful
Posted on 9/4/2015 3:04:46 PM |
lele52099 posted on 2015-9-4 13:20
It's so powerful

Why do you have this expression?
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com