Guest User

Untitled

a guest
Jan 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. import android.graphics.Bitmap;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.nio.ByteBuffer;
  7. import java.nio.CharBuffer;
  8. import java.nio.charset.CharacterCodingException;
  9. import java.nio.charset.Charset;
  10. import java.nio.charset.CharsetEncoder;
  11. import java.util.Hashtable;
  12.  
  13. import com.google.zxing.EncodeHintType;
  14. import com.google.zxing.MultiFormatWriter;
  15. import com.google.zxing.client.j2se.MatrixToImageWriter;
  16. import com.google.zxing.common.*;
  17. import com.google.zxing.qrcode.QRCodeWriter;
  18.  
  19. public class Test3 {
  20.  
  21.  
  22. public static void syncEncodeQRCode() {
  23. Charset charset = Charset.forName("UTF-8");
  24. CharsetEncoder encoder = charset.newEncoder();
  25. byte[] b = null;
  26. try {
  27. // Convert a string to UTF-8 bytes in a ByteBuffer
  28. ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("utf 8
  29. characters - i used hebrew, but you should write some of your own
  30. language characters"));
  31. b = bbuf.array();
  32. } catch (CharacterCodingException e) {
  33. System.out.println(e.getMessage());
  34. }
  35.  
  36. String data;
  37. try {
  38. data = new String(b, "UTF-8");
  39. // get a byte matrix for the data
  40. BitMatrix matrix = null;
  41. int h = 100;
  42. int w = 100;
  43. com.google.zxing.Writer writer = new MultiFormatWriter();
  44. try {
  45. Hashtable<EncodeHintType, String> hints = new
  46. Hashtable<EncodeHintType, String>(2);
  47. hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
  48. matrix = new QRCodeWriter().encode(data,
  49. com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints);
  50. } catch (com.google.zxing.WriterException e) {
  51. System.out.println(e.getMessage());
  52. }
  53.  
  54. // change this path to match yours (this is my mac home folder,
  55. you can use: c:\qr_png.png if you are on windows)
  56. String filePath = "/Users/shaybc/Desktop/OutlookQR/qr_png.png";
  57. File file = new File(filePath);
  58. try {
  59. MatrixToImageWriter.writeToFile(matrix, "PNG", file);
  60. System.out.println("printing to " + file.getAbsolutePath());
  61. } catch (IOException e) {
  62. System.out.println(e.getMessage());
  63. }
  64. } catch (UnsupportedEncodingException e) {
  65. System.out.println(e.getMessage());
  66. }
  67. }
  68. }
Add Comment
Please, Sign In to add comment