Guest User

Untitled

a guest
Sep 9th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. package com.test.myapplication;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Color;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.Menu;
  8. import android.view.MenuItem;
  9. import android.widget.ImageView;
  10.  
  11. import com.google.zxing.BarcodeFormat;
  12. import com.google.zxing.WriterException;
  13. import com.google.zxing.common.BitMatrix;
  14. import com.google.zxing.qrcode.QRCodeWriter;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.     private ImageView mQrcodeImage;
  18.  
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.         mQrcodeImage = (ImageView) findViewById(R.id.qrcodeImage);
  24.         QRCodeWriter writer = new QRCodeWriter();
  25.         try {
  26.             BitMatrix matrix = writer.encode("testtest",
  27.                     BarcodeFormat.QR_CODE, 600, 600);
  28.             mQrcodeImage.setImageBitmap(toBitmap(matrix));
  29.         } catch (WriterException e) {
  30.             e.printStackTrace();
  31.         }
  32.     }
  33.  
  34.     @Override
  35.     public boolean onCreateOptionsMenu(Menu menu) {
  36.         // Inflate the menu; this adds items to the action bar if it is present.
  37.         getMenuInflater().inflate(R.menu.menu_main, menu);
  38.         return true;
  39.     }
  40.  
  41.     @Override
  42.     public boolean onOptionsItemSelected(MenuItem item) {
  43.         // Handle action bar item clicks here. The action bar will
  44.         // automatically handle clicks on the Home/Up button, so long
  45.         // as you specify a parent activity in AndroidManifest.xml.
  46.         int id = item.getItemId();
  47.  
  48.         //noinspection SimplifiableIfStatement
  49.         if (id == R.id.action_settings) {
  50.             return true;
  51.         }
  52.  
  53.         return super.onOptionsItemSelected(item);
  54.     }
  55.  
  56.     public Bitmap toBitmap(BitMatrix matrix) {
  57.         int height = matrix.getHeight();
  58.         int width = matrix.getWidth();
  59.         Bitmap bmp = Bitmap
  60.                 .createBitmap(width, height, Bitmap.Config.ARGB_8888);
  61.         for (int x = 0; x < width; x++) {
  62.             for (int y = 0; y < height; y++) {
  63.                 bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK
  64.                         : Color.WHITE);
  65.             }
  66.         }
  67.         return bmp;
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment