Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.78 KB | None | 0 0
  1. package com.example.user.stockmanagementsystem;
  2.  
  3.  
  4. import android.graphics.Bitmap;
  5. import android.media.MediaScannerConnection;
  6. import android.os.Environment;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15.  
  16. import com.google.zxing.BarcodeFormat;
  17. import com.google.zxing.MultiFormatWriter;
  18. import com.google.zxing.WriterException;
  19. import com.google.zxing.common.BitMatrix;
  20.  
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.util.Calendar;
  26.  
  27. public class GenerateQR extends AppCompatActivity {
  28.  
  29.     public final static int QRcodeWidth = 500 ;
  30.     private static final String IMAGE_DIRECTORY = "/QRcodeDemonuts";
  31.     Bitmap bitmap ;
  32.     private EditText etqr;
  33.     private ImageView iv;
  34.     private Button btngenerate,btnback;
  35.  
  36.     @Override
  37.     protected void onCreate(Bundle savedInstanceState) {
  38.         super.onCreate(savedInstanceState);
  39.         setContentView(R.layout.generateqr);
  40.  
  41.         iv = (ImageView) findViewById(R.id.qrcode);
  42.         etqr = (EditText) findViewById(R.id.etqr);
  43.         btnback = (Button) findViewById(R.id.btnback);
  44.         btngenerate = (Button) findViewById(R.id.btngenerate);
  45.         btnback.setOnClickListener(new View.OnClickListener() {
  46.             @Override
  47.             public void onClick(View v) {
  48.                 OnBack();
  49.             }
  50.         });
  51.  
  52.         btngenerate.setOnClickListener(new View.OnClickListener() {
  53.             @Override
  54.             public void onClick(View v) {
  55.                 if(etqr.getText().toString().trim().length() == 0){
  56.                     Toast.makeText(GenerateQR.this, "Enter String!", Toast.LENGTH_SHORT).show();
  57.                 }else {
  58.                     try {
  59.                         bitmap = TextToImageEncode(etqr.getText().toString());
  60.                         iv.setImageBitmap(bitmap);
  61.                         String path = saveImage(bitmap);  //give read write permission
  62.                         Toast.makeText(GenerateQR.this, "QRCode saved to -> "+path, Toast.LENGTH_SHORT).show();
  63.                     } catch (WriterException e) {
  64.                         e.printStackTrace();
  65.                     }
  66.  
  67.                 }
  68.             }
  69.         });
  70.  
  71.  
  72.  
  73.     }
  74.  
  75.     public String saveImage(Bitmap myBitmap) {
  76.         ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  77.         myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
  78.         File wallpaperDirectory = new File(
  79.                 Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
  80.         // have the object build the directory structure, if needed.
  81.         if (!wallpaperDirectory.exists()) {
  82.             wallpaperDirectory.mkdirs();
  83.         }
  84.  
  85.         try {
  86.             File f = new File(wallpaperDirectory, Calendar.getInstance()
  87.                     .getTimeInMillis() + ".jpg");
  88.             f.createNewFile();
  89.             FileOutputStream fo = new FileOutputStream(f);
  90.             fo.write(bytes.toByteArray());
  91.             MediaScannerConnection.scanFile(this,
  92.                     new String[]{f.getPath()},
  93.                     new String[]{"image/jpeg"}, null);
  94.             fo.close();
  95.             Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
  96.  
  97.             return f.getAbsolutePath();
  98.         } catch (IOException e1) {
  99.             e1.printStackTrace();
  100.         }
  101.         return "";
  102.  
  103.     }
  104.     private Bitmap TextToImageEncode(String Value) throws WriterException {
  105.         BitMatrix bitMatrix;
  106.         try {
  107.             bitMatrix = new MultiFormatWriter().encode(
  108.                     Value,
  109.                     BarcodeFormat.DATA_MATRIX.QR_CODE,
  110.                     QRcodeWidth, QRcodeWidth, null
  111.             );
  112.  
  113.         } catch (IllegalArgumentException Illegalargumentexception) {
  114.  
  115.             return null;
  116.         }
  117.         int bitMatrixWidth = bitMatrix.getWidth();
  118.  
  119.         int bitMatrixHeight = bitMatrix.getHeight();
  120.  
  121.         int[] pixels = new int[bitMatrixWidth * bitMatrixHeight];
  122.  
  123.         for (int y = 0; y < bitMatrixHeight; y++) {
  124.             int offset = y * bitMatrixWidth;
  125.  
  126.             for (int x = 0; x < bitMatrixWidth; x++) {
  127.  
  128.                 pixels[offset + x] = bitMatrix.get(x, y) ?
  129.                         getResources().getColor(R.color.black):getResources().getColor(R.color.white);
  130.             }
  131.         }
  132.         Bitmap bitmap = Bitmap.createBitmap(bitMatrixWidth, bitMatrixHeight, Bitmap.Config.ARGB_4444);
  133.  
  134.         bitmap.setPixels(pixels, 0, 500, 0, 0, bitMatrixWidth, bitMatrixHeight);
  135.         return bitmap;
  136.     }
  137.  
  138.     public void OnBack() {
  139.         finish();
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement