Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.myapplication;
- import android.graphics.Bitmap;
- import android.graphics.Color;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ImageView;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.WriterException;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.QRCodeWriter;
- public class MainActivity extends AppCompatActivity {
- private ImageView mQrcodeImage;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mQrcodeImage = (ImageView) findViewById(R.id.qrcodeImage);
- QRCodeWriter writer = new QRCodeWriter();
- try {
- BitMatrix matrix = writer.encode("testtest",
- BarcodeFormat.QR_CODE, 600, 600);
- mQrcodeImage.setImageBitmap(toBitmap(matrix));
- } catch (WriterException e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public Bitmap toBitmap(BitMatrix matrix) {
- int height = matrix.getHeight();
- int width = matrix.getWidth();
- Bitmap bmp = Bitmap
- .createBitmap(width, height, Bitmap.Config.ARGB_8888);
- for (int x = 0; x < width; x++) {
- for (int y = 0; y < height; y++) {
- bmp.setPixel(x, y, matrix.get(x, y) ? Color.BLACK
- : Color.WHITE);
- }
- }
- return bmp;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment