To center the image usage code, as in my activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/myImage" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout>

To generate and display a QR code usage code, as in my MainActivity.java :
public class MainActivity extends AppCompatActivity { public final static int WHITE = 0xFFFFFFFF; public final static int BLACK = 0xFF000000; public final static int WIDTH = 400; public final static int HEIGHT = 400; public final static String STR = "A string to be encoded as QR code"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = (ImageView) findViewById(R.id.myImage); try { Bitmap bitmap = encodeAsBitmap(STR); imageView.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } Bitmap encodeAsBitmap(String str) throws WriterException { BitMatrix result; try { result = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, null); } catch (IllegalArgumentException iae) {
In Android Studio, add the following line to the build.gradle file:
dependencies { .... compile 'com.google.zxing:core:3.2.1' }
Or - if you still use Eclipse with the ADT plugin , add core.jar ZXing to the libs subdirectory (here fullscreen ):

Alexander Farber May 29 '15 at 12:22 2015-05-29 12:22
source share