Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use zxing library to generate barcode easily.</p> <p>first, locate core.jar under libs folder.</p> <pre><code>libs/core.jar </code></pre> <p>You can download ZXing-2.1.zip from here.</p> <p><a href="http://repo1.maven.org/maven2/com/google/zxing/" rel="noreferrer">http://repo1.maven.org/maven2/com/google/zxing/</a> (<a href="https://github.com/zxing/zxing/releases" rel="noreferrer">source</a>)</p> <p>After unzipping the file, find the jar file.</p> <pre><code>\ZXing-2.1\zxing-2.1\core\core.jar </code></pre> <p>And then write your own code like below.</p> <pre><code>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&lt;EncodeHintType, Object&gt; hints = null; String encoding = guessAppropriateEncoding(contentsToEncode); if (encoding != null) { hints = new EnumMap&lt;EncodeHintType, Object&gt;(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 &lt; height; y++) { int offset = y * width; for (int x = 0; x &lt; 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 &lt; contents.length(); i++) { if (contents.charAt(i) &gt; 0xFF) { return "UTF-8"; } } return null; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload