Skip to content

Commit b731fc0

Browse files
author
open-android
committed
add jitpack
1 parent 1301c99 commit b731fc0

File tree

7 files changed

+73
-43
lines changed

7 files changed

+73
-43
lines changed

README.MD

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,29 @@
77
![](screenshot1.gif)
88

99
![](screenshot2.gif)
10+
11+
## 使用步骤
12+
13+
### 1. 在project的build.gradle添加如下代码(如下图)
14+
15+
allprojects {
16+
repositories {
17+
maven { url "https://jitpack.io" }
18+
}
19+
}
20+
21+
![](tu_1.png)
22+
23+
### 2. 在Module的build.gradle添加依赖
24+
25+
compile 'com.github.open-android:Zxing:v1.0.0'
26+
27+
### 3.清单文件添加权限
28+
29+
<uses-permission android:name="android.permission.CAMERA" />
30+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
31+
<uses-feature android:name="android.hardware.camera" />
32+
<uses-feature android:name="android.hardware.camera.autofocus" />
33+
<uses-permission android:name="android.permission.VIBRATE" />
34+
<uses-permission android:name="android.permission.FLASHLIGHT" />
35+

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ dependencies {
2626
})
2727
compile 'com.android.support:appcompat-v7:25.0.0'
2828
testCompile 'junit:junit:4.12'
29-
compile project(':zxinglib')
29+
compile 'com.github.open-android:Zxing:v1.0.0'
3030
}

app/src/main/java/com/itheima/zxingdemo/MainActivity.java

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@
99
import android.widget.EditText;
1010
import android.widget.ImageView;
1111

12-
import com.google.zxing.BarcodeFormat;
13-
import com.google.zxing.EncodeHintType;
14-
import com.google.zxing.MultiFormatWriter;
1512
import com.google.zxing.WriterException;
1613
import com.google.zxing.activity.CaptureActivity;
17-
import com.google.zxing.common.BitMatrix;
18-
19-
import java.util.Hashtable;
14+
import com.google.zxing.common.BitmapUtils;
2015

2116
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
2217

@@ -51,44 +46,15 @@ public void onClick(View v) {
5146
break;
5247
case R.id.btn2:
5348
String content = mEt.getText().toString().trim();
54-
try {
55-
Bitmap bitmap = Create2DCode(content);
56-
mImage.setImageBitmap(bitmap);
57-
} catch (WriterException e) {
58-
e.printStackTrace();
59-
}
60-
break;
49+
// try {
50+
// Bitmap bitmap = null;
51+
// mImage.setImageBitmap(bitmap);
52+
// } catch (WriterException e) {
53+
// e.printStackTrace();
54+
// }
55+
// break;
6156
}
6257
}
6358

64-
/**
65-
* 用字符串生成二维码
66-
*
67-
* @param str
68-
* @return
69-
* @throws WriterException
70-
*/
71-
public Bitmap Create2DCode(String str) throws WriterException {
72-
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
73-
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
74-
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
75-
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
76-
int width = matrix.getWidth();
77-
int height = matrix.getHeight();
78-
// 二维矩阵转为一维像素数组,也就是一直横着排了
79-
int[] pixels = new int[width * height];
80-
for (int y = 0; y < height; y++) {
81-
for (int x = 0; x < width; x++) {
82-
if (matrix.get(x, y)) {
83-
pixels[y * width + x] = 0xff000000;
84-
}
85-
}
86-
}
87-
88-
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
89-
// 通过像素数组生成bitmap,具体参考api
90-
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
91-
return bitmap;
92-
}
9359

9460
}

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ buildscript {
1616
allprojects {
1717
repositories {
1818
jcenter()
19+
maven { url 'https://jitpack.io' }
1920
}
2021
}
2122

tu_1.png

45.4 KB
Loading

tu_2.png

56.5 KB
Loading

zxinglib/src/main/java/com/google/zxing/common/BitmapUtils.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
import android.graphics.Bitmap;
55
import android.graphics.BitmapFactory;
66

7+
import com.google.zxing.BarcodeFormat;
8+
import com.google.zxing.EncodeHintType;
9+
import com.google.zxing.MultiFormatWriter;
10+
import com.google.zxing.WriterException;
11+
12+
import java.util.Hashtable;
13+
714
public class BitmapUtils {
815

916
public static Bitmap decodeSampledBitmapFromResource(Resources res,
@@ -56,4 +63,34 @@ public static Bitmap getCompressedBitmap(String path) {
5663
options.inJustDecodeBounds = false;
5764
return BitmapFactory.decodeFile(path, options);
5865
}
66+
67+
/**
68+
* 用字符串生成二维码
69+
*
70+
* @param str
71+
* @return
72+
* @throws WriterException
73+
*/
74+
public static Bitmap create2DCode(String str) throws WriterException {
75+
Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
76+
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
77+
// 生成二维矩阵,编码时指定大小,不要生成了图片以后再进行缩放,这样会模糊导致识别失败
78+
BitMatrix matrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, 480, 480, hints);
79+
int width = matrix.getWidth();
80+
int height = matrix.getHeight();
81+
// 二维矩阵转为一维像素数组,也就是一直横着排了
82+
int[] pixels = new int[width * height];
83+
for (int y = 0; y < height; y++) {
84+
for (int x = 0; x < width; x++) {
85+
if (matrix.get(x, y)) {
86+
pixels[y * width + x] = 0xff000000;
87+
}
88+
}
89+
}
90+
91+
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
92+
// 通过像素数组生成bitmap,具体参考api
93+
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
94+
return bitmap;
95+
}
5996
}

0 commit comments

Comments
 (0)