【问题标题】:How to get BinaryBitmap from imageView如何从 imageView 获取 BinaryBitmap
【发布时间】:2018-03-13 19:45:17
【问题描述】:

我想解码设置到 imageView 中的二维码。 我尝试了以下代码。

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();

    QRCodeReader reader = new QRCodeReader();
    Result result = reader.decode(bitmap);

但它说它需要 BinaryBitmap 而不是 Bitmap。 我应该怎么办?

提前致谢。

【问题讨论】:

  • 谁说的?什么BinaryBitmap
  • 或许你可以看看this answer
  • reader.decode() 想要 binarybitmap 作为 peramiter
  • 检查这个stackoverflow.com/questions/14861553/…,它可以帮助我解决过去的那个问题。
  • 非常感谢@educanovas93。它有效

标签: java android qr-code


【解决方案1】:

您可以使用 ZXing 库中的 MultiFormatReader 这个类。

Bitmap bMap = [...];
String contents = null;

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());  

LuminanceSource source = new RGBLuminanceSource(bMap.getWidth(), bMap.getHeight(), intArray);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

Reader reader = new MultiFormatReader();
Result result = reader.decode(bitmap);
contents = result.getText();

【讨论】:

    【解决方案2】:

    我认为它是指您需要将 byte64 转换后的图像(位图到字节串)传递的事实

    你可以这样做:

    private String bitmapToBase64(Bitmap bitmap) {
       ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
       byte[] byteArray = byteArrayOutputStream .toByteArray();
       return Base64.encodeToString(byteArray, Base64.DEFAULT);
    }
    

    之后,如果您需要从字节数组中取回位图,您可以像这样反转过程:

    private Bitmap base64ToBitmap(String b64) {
       byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT);
       return BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
    }
    

    【讨论】:

      【解决方案3】:
      add library to app.gradle
      
      compile 'com.google.zxing:core:3.2.1'
      compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
      
          In the MainActivity use following code 
      
              public class MainActivity extends AppCompatActivity {
                ImageView imageView;
                Button button;
                Button btnScan;
                EditText editText;
                String EditTextValue ;
                Thread thread ;
                public final static int QRcodeWidth = 350 ;
                Bitmap bitmap ;
      
                TextView tv_qr_readTxt;
      
               @Override
               protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
      
                  imageView = (ImageView)findViewById(R.id.imageView);
                  editText = (EditText)findViewById(R.id.editText);
                  button = (Button)findViewById(R.id.button);
                  btnScan = (Button)findViewById(R.id.btnScan);
                   tv_qr_readTxt = (TextView) findViewById(R.id.tv_qr_readTxt);
      
              button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
      
      
      
                      if(!editText.getText().toString().isEmpty()){
                          EditTextValue = editText.getText().toString();
      
                          try {
                              bitmap = TextToImageEncode(EditTextValue);
      
                              imageView.setImageBitmap(bitmap);
      
                          } catch (WriterException e) {
                              e.printStackTrace();
                          }
                      }
                      else{
                          editText.requestFocus();
                          Toast.makeText(MainActivity.this, "Please Enter Your Scanned Test" , Toast.LENGTH_LONG).show();
                      }
      
                    }
                });
      
      
              btnScan.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
      
                      IntentIntegrator integrator = new IntentIntegrator(MainActivity.this);
                      integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
                      integrator.setPrompt("Scan");
                      integrator.setCameraId(0);
                      integrator.setBeepEnabled(false);
                      integrator.setBarcodeImageEnabled(false);
                      integrator.initiateScan();
      
                   }
                 });
                  }
      
      
               Bitmap TextToImageEncode(String Value) throws WriterException {
                 BitMatrix bitMatrix;
                  try {
                  bitMatrix = new MultiFormatWriter().encode(
                          Value,
                          BarcodeFormat.DATA_MATRIX.QR_CODE,
                          QRcodeWidth, QRcodeWidth, null
                  );
      
                  } catch (IllegalArgumentException Illegalargumentexception) {
      
                   return null;
                 }
                int bitMatrixWidth = bitMatrix.getWidth();
      
                int bitMatrixHeight = bitMatrix.getHeight();
      
                int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
      
                for (int y = 0; y < bitMatrixHeight; y++) {
                    int offset = y * bitMatrixWidth;
      
                   for (int x = 0; x < bitMatrixWidth; x++) {
      
                       pixels[offset + x] = bitMatrix.get(x, y) ?
                              getResources().getColor(R.color.QRCodeBlackColor):getResources().getColor(R.color.QRCodeWhiteColor);
                    }
                  }
                  Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
      
                 bitmap.setPixels(pixels, 0, 350, 0, 0, bitMatrixWidth, bitMatrixHeight);
                 return bitmap;
              }
      
      
      
      
                @Override
                protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                  IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                if(result != null) {
                  if(result.getContents() == null) {
                      Log.e("Scan*******", "Cancelled scan");
      
                   } else {
                      Log.e("Scan", "Scanned");
      
                      tv_qr_readTxt.setText(result.getContents());
                      Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
                   }
                } else {
                    // This is important, otherwise the result will not be passed to the fragment
                  super.onActivityResult(requestCode, resultCode, data);
                   }
                 }
               }
      

      【讨论】:

        猜你喜欢
        • 2022-12-05
        • 1970-01-01
        • 2014-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-17
        相关资源
        最近更新 更多