Last active
August 29, 2015 14:13
-
-
Save Kun-Yao-Lin/9e4205d293f25908dd8b to your computer and use it in GitHub Desktop.
AS Workshop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <ImageView xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:id="@+id/qr_code" | |
| android:layout_gravity="center" | |
| android:scaleType="fitCenter" | |
| android:background="@android:color/transparent" /> | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apply plugin: 'com.android.application' | |
| android { | |
| compileSdkVersion 21 | |
| buildToolsVersion "21.1.1" | |
| defaultConfig { | |
| applicationId "tw.com.akdg.helloandroidstudio" | |
| minSdkVersion 15 | |
| targetSdkVersion 21 | |
| versionCode 1 | |
| versionName "1.0" | |
| } | |
| buildTypes { | |
| release { | |
| minifyEnabled false | |
| proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
| } | |
| } | |
| } | |
| dependencies { | |
| compile fileTree(dir: 'libs', include: ['*.jar']) | |
| compile 'com.android.support:appcompat-v7:21.0.3' | |
| //QR | |
| compile 'com.google.zxing:javase:2.0' | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package tw.com.akdg.helloandroidstudio; | |
| import android.app.Activity; | |
| import android.graphics.Bitmap; | |
| import android.os.Bundle; | |
| import android.util.DisplayMetrics; | |
| import android.util.Log; | |
| import android.widget.ImageView; | |
| import com.google.zxing.EncodeHintType; | |
| import java.util.HashMap; | |
| public class MainActivity extends Activity { | |
| public static final String TAG = "MainActivity"; | |
| private DisplayMetrics metrics; | |
| private ImageView QR_code; | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| metrics = this.getResources().getDisplayMetrics(); | |
| QR_code = (ImageView) this.findViewById(R.id.qr_code); | |
| try { | |
| createQRcode("http://goo.gl/2SJIkk"); | |
| } catch (Exception e) { | |
| Log.e(TAG, "createQRcode failed", e); | |
| } | |
| } | |
| private void createQRcode(final String content) throws Exception { | |
| try { | |
| final Bitmap bitmap = encodeAsBitmap(content, com.google.zxing.BarcodeFormat.QR_CODE, metrics.widthPixels, metrics.widthPixels); | |
| if (null != bitmap) { | |
| QR_code.setImageBitmap(bitmap); | |
| } | |
| } catch (Exception e) { | |
| throw e; | |
| } finally { | |
| } | |
| } | |
| private Bitmap encodeAsBitmap(String contents, com.google.zxing.BarcodeFormat format, | |
| int desiredWidth, int desiredHeight) throws com.google.zxing.WriterException { | |
| final int WHITE = this.getResources().getColor(android.R.color.transparent); | |
| final int BLACK = 0xFF000000; | |
| HashMap<EncodeHintType, String> hints = null; | |
| String encoding = guessAppropriateEncoding(contents); | |
| if (encoding != null) { | |
| hints = new HashMap<EncodeHintType, String>(2); | |
| hints.put(com.google.zxing.EncodeHintType.CHARACTER_SET, encoding); | |
| } | |
| com.google.zxing.MultiFormatWriter writer = new com.google.zxing.MultiFormatWriter(); | |
| com.google.zxing.common.BitMatrix result = writer.encode(contents, format, desiredWidth, | |
| desiredHeight, 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 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment