Skip to content

Commit aebb422

Browse files
author
open-android
committed
增加扫描本地二维码图片
1 parent 97f0178 commit aebb422

File tree

13 files changed

+111
-63
lines changed

13 files changed

+111
-63
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.MD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
![](screenshot2.gif)
1010

11+
![](screenshot3.gif)
12+
1113
## 使用步骤
1214

1315
### 1. 在project的build.gradle添加如下代码(如下图)

app/build.gradle

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

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

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.itheima.zxingdemo;
22

3+
import android.content.Context;
34
import android.content.Intent;
45
import android.graphics.Bitmap;
56
import android.os.Bundle;
@@ -8,6 +9,8 @@
89
import android.widget.Button;
910
import android.widget.EditText;
1011
import android.widget.ImageView;
12+
import android.widget.TextView;
13+
import android.widget.Toast;
1114

1215
import com.google.zxing.WriterException;
1316
import com.google.zxing.activity.CaptureActivity;
@@ -19,12 +22,17 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
1922
private EditText mEt;
2023
private Button mBtn2;
2124
private ImageView mImage;
25+
private final static int REQ_CODE = 1028;
26+
private Context mContext;
27+
private TextView mTvResult;
28+
private ImageView mImageCallback;
2229

2330
@Override
2431
protected void onCreate(Bundle savedInstanceState) {
2532
super.onCreate(savedInstanceState);
2633
setContentView(R.layout.activity_main);
2734
initView();
35+
mContext = this;
2836
}
2937

3038
private void initView() {
@@ -36,19 +44,31 @@ private void initView() {
3644
mBtn2.setOnClickListener(this);
3745
mImage = (ImageView) findViewById(R.id.image);
3846
mImage.setOnClickListener(this);
47+
mTvResult = (TextView) findViewById(R.id.tv_result);
48+
mTvResult.setOnClickListener(this);
49+
mImageCallback = (ImageView) findViewById(R.id.image_callback);
50+
mImageCallback.setOnClickListener(this);
3951
}
4052

4153
@Override
4254
public void onClick(View v) {
4355
switch (v.getId()) {
4456
case R.id.btn1:
45-
startActivity(new Intent(MainActivity.this, CaptureActivity.class));
57+
// startActivity(new Intent(MainActivity.this, CaptureActivity.class));
58+
Intent intent = new Intent(mContext, CaptureActivity.class);
59+
startActivityForResult(intent, REQ_CODE);
4660
break;
4761
case R.id.btn2:
62+
mImage.setVisibility(View.VISIBLE);
63+
//隐藏扫码结果view
64+
mImageCallback.setVisibility(View.GONE);
65+
mTvResult.setVisibility(View.GONE);
66+
4867
String content = mEt.getText().toString().trim();
4968
Bitmap bitmap = null;
5069
try {
51-
bitmap = BitmapUtils.create2DCode(content);
70+
bitmap = BitmapUtils.create2DCode(content);//根据内容生成二维码
71+
mTvResult.setVisibility(View.GONE);
5272
mImage.setImageBitmap(bitmap);
5373
} catch (WriterException e) {
5474
e.printStackTrace();
@@ -57,5 +77,28 @@ public void onClick(View v) {
5777
}
5878
}
5979

80+
@Override
81+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
82+
super.onActivityResult(requestCode, resultCode, data);
83+
if (requestCode == REQ_CODE) {
84+
mImage.setVisibility(View.GONE);
85+
mTvResult.setVisibility(View.VISIBLE);
86+
mImageCallback.setVisibility(View.VISIBLE);
87+
88+
String result = data.getStringExtra(CaptureActivity.SCAN_QRCODE_RESULT);
89+
Bitmap bitmap = data.getParcelableExtra(CaptureActivity.SCAN_QRCODE_BITMAP);
90+
91+
mTvResult.setText("扫码结果:"+result);
92+
showToast("扫码结果:" + result);
93+
if(bitmap != null){
94+
mImageCallback.setImageBitmap(bitmap);//现实扫码图片
95+
}
96+
}
6097

98+
99+
}
100+
101+
private void showToast(String msg) {
102+
Toast.makeText(mContext, "" + msg, Toast.LENGTH_SHORT).show();
103+
}
61104
}

app/src/main/res/layout/activity_main.xml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<EditText
88
android:id="@+id/et"
99
android:layout_width="match_parent"
10-
android:text="http://baidu.com"
1110
android:layout_height="wrap_content"
12-
android:hint="输入内容,生成二维码" />
11+
android:hint="输入内容,生成二维码"
12+
android:text="http://baidu.com" />
1313

1414
<Button
1515
android:id="@+id/btn2"
@@ -25,9 +25,27 @@
2525

2626
<ImageView
2727
android:id="@+id/image"
28-
android:layout_gravity="center_horizontal"
2928
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:layout_gravity="center_horizontal" />
31+
32+
33+
<TextView
34+
android:id="@+id/tv_result"
35+
android:text="扫描返回bitmap"
36+
android:gravity="center"
37+
android:textSize="18sp"
38+
android:textColor="#FF0000"
39+
android:layout_marginTop="30dp"
40+
android:layout_width="match_parent"
3041
android:layout_height="wrap_content" />
3142

43+
<ImageView
44+
android:id="@+id/image_callback"
45+
android:layout_marginTop="10dp"
46+
android:layout_width="wrap_content"
47+
android:layout_height="wrap_content"
48+
android:layout_gravity="center_horizontal" />
49+
3250

3351
</LinearLayout>

gradle.properties

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,22 @@
33
# IDE (e.g. Android Studio) users:
44
# Gradle settings configured through the IDE *will override*
55
# any settings specified in this file.
6+
org.gradle.daemon=true
67

78
# For more details on how to configure your build environment visit
89
# http://www.gradle.org/docs/current/userguide/build_environment.html
910

1011
# Specifies the JVM arguments used for the daemon process.
1112
# The setting is particularly useful for tweaking memory settings.
12-
org.gradle.jvmargs=-Xmx1536m
13+
# Default value: -Xmx10248m -XX:MaxPermSize=256m
14+
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
1315

1416
# When configured, Gradle will run in incubating parallel mode.
1517
# This option should only be used with decoupled projects. More details, visit
1618
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17-
# org.gradle.parallel=true
19+
org.gradle.parallel=true
20+
21+
# Enables new incubating mode that makes Gradle selective when configuring projects.
22+
# Only relevant projects are configured which results in faster builds for large multi-projects.
23+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:configuration_on_demand
24+
org.gradle.configureondemand=true

screenshot1.gif

58.2 KB
Loading

screenshot2.gif

-587 KB
Loading

screenshot3.gif

2.57 MB
Loading

zxinglib/build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,5 @@ android {
2525

2626
dependencies {
2727
compile fileTree(dir: 'libs', include: ['*.jar'])
28-
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
29-
exclude group: 'com.android.support', module: 'support-annotations'
30-
})
3128
compile 'com.android.support:appcompat-v7:25.0.0'
32-
testCompile 'junit:junit:4.12'
3329
}

0 commit comments

Comments
 (0)