5

बारकोड कैसे उत्पन्न करें और इसे नए Google विज़न API का उपयोग करके बिटमैप में परिवर्तित करें?बारकोड कैसे उत्पन्न करें और इसे नए Google विज़न एपीआई का उपयोग करके बिटमैप में परिवर्तित करें?

Barcode barcode = new Barcode(); 
Barcode.Email email = new Barcode.Email(); 
email.address = "[email protected]"; 
email.subject = "My Subject; 
email.body = "My body content."; 
barcode.email = email; 

// रूपांतरण लागू बिटमैप barcodeImage = barcodeToBitmap (बारकोड); // मैं इस हिस्से में पता है।

+0

हाँ, कृपया मंच टैग जोड़ने या अपने प्रश्न जो मोबाइल प्लेटफार्मों उस पर चल पाएंगे में वर्णन: आप कुछ इस तरह की कोशिश कर सकते हैं, यह ZXing पुस्तकालय का उपयोग करता है। – prasun

+0

ठीक है। यह एंड्रॉइड के बारे में है। – Joyofio987

+0

मैं सोच रहा था कि यदि नई Google play सेवा रूपांतरण करने में असमर्थ है, तो मुझे जेडएक्सिंग लाइब्रेरी जैसी तीसरी पार्टी लाइब्रेरी का उपयोग करना होगा, मुझे लगता है कि मुझे ईवेंट एपीआई के साथ नई Google Play सेवा क्यों चाहिए। – Joyofio987

उत्तर

6

आप Google विजन एपीआई का उपयोग करके अपने बारकोड का पता लगा सकते हैं और फिर बारकोड उत्पन्न करने के लिए जेएक्सिंग का उपयोग कर सकते हैं।

public static Bitmap getBitmap(String barcode, int barcodeType, int width, int height) 
{ 
    Bitmap barcodeBitmap = null; 
    BarcodeFormat barcodeFormat = convertToZXingFormat(barcodeType); 
    try 
    { 
     barcodeBitmap = encodeAsBitmap(barcode, barcodeFormat, width, height); 
    } 
    catch (WriterException e) 
    { 
     e.printStackTrace(); 
    } 
    return barcodeBitmap; 
} 

private static BarcodeFormat convertToZXingFormat(int format) 
{ 
    switch (format) 
    { 
     case 8: 
      return BarcodeFormat.CODABAR; 
     case 1: 
      return BarcodeFormat.CODE_128; 
     case 2: 
      return BarcodeFormat.CODE_39; 
     case 4: 
      return BarcodeFormat.CODE_93; 
     case 32: 
      return BarcodeFormat.EAN_13; 
     case 64: 
      return BarcodeFormat.EAN_8; 
     case 128: 
      return BarcodeFormat.ITF; 
     case 512: 
      return BarcodeFormat.UPC_A; 
     case 1024: 
      return BarcodeFormat.UPC_E; 
     //default 128? 
     default: 
      return BarcodeFormat.CODE_128; 
    } 
} 


/************************************************************** 
* getting from com.google.zxing.client.android.encode.QRCodeEncoder 
* 
* See the sites below 
* http://code.google.com/p/zxing/ 
* http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/EncodeActivity.java 
* http://code.google.com/p/zxing/source/browse/trunk/android/src/com/google/zxing/client/android/encode/QRCodeEncoder.java 
*/ 

private static final int WHITE = 0xFFFFFFFF; 
private static final int BLACK = 0xFF000000; 

private static Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException 
{ 
    if (contents == null) { 
     return null; 
    } 
    Map<EncodeHintType, Object> hints = null; 
    String encoding = guessAppropriateEncoding(contents); 
    if (encoding != null) { 
     hints = new EnumMap<>(EncodeHintType.class); 
     hints.put(EncodeHintType.CHARACTER_SET, encoding); 
    } 
    MultiFormatWriter writer = new MultiFormatWriter(); 
    BitMatrix result; 
    try { 
     result = writer.encode(contents, format, img_width, img_height, hints); 
    } catch (IllegalArgumentException iae) { 
     // Unsupported format 
     return null; 
    } 
    int width = result.getWidth(); 
    int height = result.getHeight(); 
    int[] pixels = new int[width * height]; 
    for (int y = 0; y < height; y++) { 
     int offset = y * width; 
     for (int x = 0; x < width; x++) { 
      pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; 
     } 
    } 

    Bitmap bitmap = Bitmap.createBitmap(width, height, 
      Bitmap.Config.ARGB_8888); 
    bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 
    return bitmap; 
} 

private static String guessAppropriateEncoding(CharSequence contents) { 
    // Very crude at the moment 
    for (int i = 0; i < contents.length(); i++) { 
     if (contents.charAt(i) > 0xFF) { 
      return "UTF-8"; 
     } 
    } 
    return null; 
} 

}

+0

ठीक है। बहुत बहुत धन्यवाद!! – Joyofio987

+2

ऊपर कोड ठीक है, लेकिन चौड़ाई और ऊंचाई के लिए उच्च मानों का उपयोग करने से सावधान रहें (बिटमैप बनाने में बहुत लंबा समय लगेगा), यह मानना ​​है कि यह मान कम रखें और बिटमैप का आकार बदलने के बजाय समझाया गया है [यहां] (http://stackoverflow.com/प्रश्न/4837715/कैसे करने वाली आकार एक बिटमैप में एंड्रॉयड) –

संबंधित मुद्दे

 संबंधित मुद्दे