Skip to content

Instantly share code, notes, and snippets.

@Kun-Yao-Lin
Created January 11, 2015 09:52
Show Gist options
  • Save Kun-Yao-Lin/881c432f64ec3a440724 to your computer and use it in GitHub Desktop.
Save Kun-Yao-Lin/881c432f64ec3a440724 to your computer and use it in GitHub Desktop.
AS Workshop - part 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/qr_code"
android:scaleType="fitCenter"
android:background="@android:color/transparent" />
<com.baoyz.widget.PullRefreshLayout
android:id="@+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</com.baoyz.widget.PullRefreshLayout>
</RelativeLayout>
apply plugin: 'com.android.application'
repositories {
flatDir {
dirs 'libs'
}
}
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'
compile(name:'library-release', ext:'aar')
}
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.baoyz.widget.PullRefreshLayout;
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);
final PullRefreshLayout layout = (PullRefreshLayout) findViewById(R.id.swipeRefreshLayout);
// listen refresh event
layout.setOnRefreshListener(new PullRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
layout.postDelayed(new Runnable() {
@Override
public void run() {
metrics = getResources().getDisplayMetrics();
QR_code = (ImageView) findViewById(R.id.qr_code);
try {
createQRcode("http://goo.gl/2SJIkk");
} catch (Exception e) {
Log.e(TAG, "createQRcode failed", e);
}
layout.setRefreshing(false);
}
}, 4000);
}
});
layout.setRefreshing(true);
layout.setRefreshStyle(PullRefreshLayout.STYLE_CIRCLES);
}
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