2012-04-27 17 views
36

मुझे 1 डी बारकोड छवि उत्पन्न करने और इसे 13-वर्ण कोड के अनुसार ImageView पर सेट करने की आवश्यकता है। क्या कोई इस संबंध में मेरी सहायता कर सकता है?एंड्रॉइड एप्लिकेशन में बारकोड छवि उत्पन्न करें

+0

वहाँ कई अलग अलग -1 डी बारकोड स्वरूप हैं ... वहाँ एक विशिष्ट एक आप उत्पन्न करने के लिए उम्मीद कर रहे हैं है? – FoamyGuy

+0

सबसे पहले, आपके उत्तर के लिए धन्यवाद ... एचएम, सुनिश्चित नहीं है कि आपका क्या मतलब है, क्या EAN13 का मतलब आपके लिए कुछ है? मुद्दा यह है कि, मुझे 13-वर्ण (संख्या वास्तव में) कोड मिल रहा है जो कोड का प्रतिनिधित्व करता है और अब मुझे छवि (खींचने योग्य) उत्पन्न करने की आवश्यकता है ताकि मैं इसे स्क्रीन पर दिखा सकूं ... बस, मुझे इसकी आवश्यकता नहीं है कोड स्वयं उत्पन्न करने के लिए, मेरे पास पहले से ही वह हिस्सा है ... – Leonidas

+0

संबंधित: http://stackoverflow.com/q/8800919/813951 –

उत्तर

0

धन्यवाद में नमूना कोड प्राप्त कर सकते हैं ... इस बीच मैं बहुत यहाँ समाधान में मैं क्या प्रयोग किया जाता है मैं तो अगर किसी के पास एक ही समस्या है तो मैं इसका उपयोग करने का सुझाव देता हूं।

फिर से धन्यवाद!

+1

आपने छवि दृश्य में बारकोड कैसे सेट किया? –

0

चेक बाहर पर जवाब: वे IText उपयोग करने का सुझाव Generate 1D barcode in Android

जो एक जावा पीडीएफ हेरफेर पुस्तकालय है। इसमें बारकोड छवियां उत्पन्न करने की क्षमता भी है। http://www.onbarcode.com/products/android_barcode/barcodes/ean13.html यह एक पुस्तकालय है कि के लिए ठीक काम किया है:

आपको लगता है कि सवाल यह है कि मैं जुड़ा हुआ अपने जवाब लोगों के लिए and also here

+0

टिम, सुझाव के लिए धन्यवाद, मैंने पहले iText के साथ प्रयास किया लेकिन मैं छुटकारा नहीं पा सका पीडीएफ दस्तावेज़ का। "CreateImage ..." विधियों का उपयोग करने के लिए, ऐसा लगता है कि आपको एक पीडीएफ बनाना है जो मेरे लिए कोई विकल्प नहीं था ... (या शायद मैंने पर्याप्त गहराई से खोला नहीं है) – Leonidas

124

आप आसानी से बारकोड उत्पन्न करने के लिए ज़क्सिंग लाइब्रेरी का उपयोग कर सकते हैं।

पहले, libs फ़ोल्डर के अंतर्गत core.jar का पता लगाएं।

libs/core.jar 

आप यहां से ZXing-2.1.zip डाउनलोड कर सकते हैं।

http://repo1.maven.org/maven2/com/google/zxing/ (source)

फ़ाइल अनज़िप करने के बाद, जार फ़ाइल पाते हैं।

\ZXing-2.1\zxing-2.1\core\core.jar 

और फिर नीचे अपना कोड लिखें।

import java.util.EnumMap; 
import java.util.Map; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.LinearLayout.LayoutParams; 
import android.widget.TextView; 

import com.google.zxing.BarcodeFormat; 
import com.google.zxing.EncodeHintType; 
import com.google.zxing.MultiFormatWriter; 
import com.google.zxing.WriterException; 
import com.google.zxing.common.BitMatrix; 

public class BarcodeExampleActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    LinearLayout l = new LinearLayout(this); 
    l.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 
    l.setOrientation(LinearLayout.VERTICAL); 

    setContentView(l); 

    // barcode data 
    String barcode_data = "123456"; 

    // barcode image 
    Bitmap bitmap = null; 
    ImageView iv = new ImageView(this); 

    try { 

     bitmap = encodeAsBitmap(barcode_data, BarcodeFormat.CODE_128, 600, 300); 
     iv.setImageBitmap(bitmap); 

    } catch (WriterException e) { 
     e.printStackTrace(); 
    } 

    l.addView(iv); 

    //barcode text 
    TextView tv = new TextView(this); 
    tv.setGravity(Gravity.CENTER_HORIZONTAL); 
    tv.setText(barcode_data); 

    l.addView(tv); 

    } 

    /************************************************************** 
    * 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; 

    Bitmap encodeAsBitmap(String contents, BarcodeFormat format, int img_width, int img_height) throws WriterException { 
    String contentsToEncode = contents; 
    if (contentsToEncode == null) { 
     return null; 
    } 
    Map<EncodeHintType, Object> hints = null; 
    String encoding = guessAppropriateEncoding(contentsToEncode); 
    if (encoding != null) { 
     hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
     hints.put(EncodeHintType.CHARACTER_SET, encoding); 
    } 
    MultiFormatWriter writer = new MultiFormatWriter(); 
    BitMatrix result; 
    try { 
     result = writer.encode(contentsToEncode, 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

+1 मैं सराहना करता हूं, अच्छी नौकरी। –

+0

उत्कृष्ट thx .. !! – fingerup

+0

superbbbbbb ..... बिल्कुल वही है जो आवश्यक है! – Sandeep

3
public Bitmap Ean13_Encode(String qrData, int qrCodeDimention) { 
    Bitmap bitmap= Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);  
    QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(qrData, null, 
    Contents.Type.TEXT, BarcodeFormat.EAN_13.toString(), qrCodeDimention); 
    try { 
     bitmap = qrCodeEncoder.encodeAsBitmap(); 
    } catch (WriterException e) { 
     e.printStackTrace(); 
    }; 
    return bitmap; 
}; 

public final class QRCodeEncoder { 
private static final int WHITE = 0xFFFFFFFF; 
private static final int BLACK = 0xFF000000; 

private int dimension = Integer.MIN_VALUE; 
private String contents = null; 
private String displayContents = null; 
private String title = null; 
private BarcodeFormat format = null; 
private boolean encoded = false; 

public QRCodeEncoder(String data, Bundle bundle, String type, String format, int dimension) { 
    this.dimension = dimension; 
    encoded = encodeContents(data, bundle, type, format); 
} 

public String getContents() { 
    return contents; 
} 

public String getDisplayContents() { 
    return displayContents; 
} 

public String getTitle() { 
    return title; 
} 

private boolean encodeContents(String data, Bundle bundle, String type, String formatString) { 
    // Default to QR_CODE if no format given. 
    format = null; 
    if (formatString != null) { 
     try { 
      format = BarcodeFormat.valueOf(formatString); 
     } catch (IllegalArgumentException iae) { 
      // Ignore it then 
     } 
    } 
    if (format == null || format == BarcodeFormat.QR_CODE) { 
     this.format = BarcodeFormat.QR_CODE; 
     encodeQRCodeContents(data, bundle, type); 
    } else if (data != null && data.length() > 0) { 
     contents = data; 
     displayContents = data; 
     title = "Text"; 
    } 
    return contents != null && contents.length() > 0; 
} 

private void encodeQRCodeContents(String data, Bundle bundle, String type) { 
    if (type.equals(Contents.Type.TEXT)) { 
     if (data != null && data.length() > 0) { 
      contents = data; 
      displayContents = data; 
      title = "Text"; 
     } 
    } else if (type.equals(Contents.Type.EMAIL)) { 
     data = trim(data); 
     if (data != null) { 
      contents = "mailto:" + data; 
      displayContents = data; 
      title = "E-Mail"; 
     } 
    } else if (type.equals(Contents.Type.PHONE)) { 
     data = trim(data); 
     if (data != null) { 
      contents = "tel:" + data; 
      displayContents = PhoneNumberUtils.formatNumber(data); 
      title = "Phone"; 
     } 
    } else if (type.equals(Contents.Type.SMS)) { 
     data = trim(data); 
     if (data != null) { 
      contents = "sms:" + data; 
      displayContents = PhoneNumberUtils.formatNumber(data); 
      title = "SMS"; 
     } 
    } else if (type.equals(Contents.Type.CONTACT)) { 
     if (bundle != null) { 
      StringBuilder newContents = new StringBuilder(100); 
      StringBuilder newDisplayContents = new StringBuilder(100); 

      newContents.append("MECARD:"); 

      String name = trim(bundle.getString(ContactsContract.Intents.Insert.NAME)); 
      if (name != null) { 
       newContents.append("N:").append(escapeMECARD(name)).append(';'); 
       newDisplayContents.append(name); 
      } 

      String address = trim(bundle.getString(ContactsContract.Intents.Insert.POSTAL)); 
      if (address != null) { 
       newContents.append("ADR:").append(escapeMECARD(address)).append(';'); 
       newDisplayContents.append('\n').append(address); 
      } 

      Collection<String> uniquePhones = new HashSet<String>(Contents.PHONE_KEYS.length); 
      for (int x = 0; x < Contents.PHONE_KEYS.length; x++) { 
       String phone = trim(bundle.getString(Contents.PHONE_KEYS[x])); 
       if (phone != null) { 
        uniquePhones.add(phone); 
       } 
      } 
      for (String phone : uniquePhones) { 
       newContents.append("TEL:").append(escapeMECARD(phone)).append(';'); 
       newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone)); 
      } 

      Collection<String> uniqueEmails = new HashSet<String>(Contents.EMAIL_KEYS.length); 
      for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) { 
       String email = trim(bundle.getString(Contents.EMAIL_KEYS[x])); 
       if (email != null) { 
        uniqueEmails.add(email); 
       } 
      } 
      for (String email : uniqueEmails) { 
       newContents.append("EMAIL:").append(escapeMECARD(email)).append(';'); 
       newDisplayContents.append('\n').append(email); 
      } 

      String url = trim(bundle.getString(Contents.URL_KEY)); 
      if (url != null) { 
       // escapeMECARD(url) -> wrong escape e.g. http\://zxing.google.com 
       newContents.append("URL:").append(url).append(';'); 
       newDisplayContents.append('\n').append(url); 
      } 

      String note = trim(bundle.getString(Contents.NOTE_KEY)); 
      if (note != null) { 
       newContents.append("NOTE:").append(escapeMECARD(note)).append(';'); 
       newDisplayContents.append('\n').append(note); 
      } 

      // Make sure we've encoded at least one field. 
      if (newDisplayContents.length() > 0) { 
       newContents.append(';'); 
       contents = newContents.toString(); 
       displayContents = newDisplayContents.toString(); 
       title = "Contact"; 
      } else { 
       contents = null; 
       displayContents = null; 
      } 

     } 
    } else if (type.equals(Contents.Type.LOCATION)) { 
     if (bundle != null) { 
      // These must use Bundle.getFloat(), not getDouble(), it's part of the API. 
      float latitude = bundle.getFloat("LAT", Float.MAX_VALUE); 
      float longitude = bundle.getFloat("LONG", Float.MAX_VALUE); 
      if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) { 
       contents = "geo:" + latitude + ',' + longitude; 
       displayContents = latitude + "," + longitude; 
       title = "Location"; 
      } 
     } 
    } 
} 

public Bitmap encodeAsBitmap() throws WriterException { 
    if (!encoded) return null; 

    Map<EncodeHintType, Object> hints = null; 
    String encoding = guessAppropriateEncoding(contents); 
    if (encoding != null) { 
     hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class); 
     hints.put(EncodeHintType.CHARACTER_SET, encoding); 
    } 
    MultiFormatWriter writer = new MultiFormatWriter(); 
    BitMatrix result = writer.encode(contents, format, dimension, dimension, hints); 
    int width = result.getWidth(); 
    int height = result.getHeight(); 
    int[] pixels = new int[width * height]; 
    // All are 0, or black, by default 
    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 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; 
} 

private String trim(String s) { 
    if (s == null) { return null; } 
    String result = s.trim(); 
    return result.length() == 0 ? null : result; 
} 

private String escapeMECARD(String input) { 
    if (input == null || (input.indexOf(':') < 0 && input.indexOf(';') < 0)) { return input; } 
    int length = input.length(); 
    StringBuilder result = new StringBuilder(length); 
    for (int i = 0; i < length; i++) { 
     char c = input.charAt(i); 
     if (c == ':' || c == ';') { 
      result.append('\\'); 
     } 
     result.append(c); 
    } 
    return result.toString(); 
} 

}

+0

मुझे नया QRCodeEncoder (qrData, null, Contents.Type.TEXT, BarcodeFormat.EAN_13.toString(), qrCodeDimention) नहीं दिखाई देता है; ZXing 2.3.0 संस्करण में कन्स्ट्रक्टर। आप किस संस्करण का उपयोग करते हैं? – tch

+0

हे टॉमसज़, मैंने क्यूआरकोडएन्कोडर का कोड जोड़ा (qrData, null, Contents.Type.TEXT, BarcodeFormat.EAN_13.toString(), qrCodeDimention); कृपया जांचें और मुझे बताएं कि क्या आपको इस ए के साथ किसी भी प्रोबलेन का सामना करना पड़ता है –

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