版权声明:本文为博主原创文章,转载请说明出处。
app中需要实现扫码功能
最近在做一个app,之前做过扫描二维码的功能。然而今天突然想要做这样一个功能时,完全想不起来当时是怎么实现的。我翻看自己的笔记本,发现太多了各种笔记了,没找到,我不知道是在其他的笔记本还是我压根没记。这时我意识到写博客对一个程序员的重要性,以后还是要养成写博客的习惯才好。
1.加载二维码开源包
compile 'cn.yipianfengye.android:zxing-library:2.0'
compile 'com.google.zxing:core:3.2.1'
2.在activity
ZXingLibrary.initDisplayOpinion(this);
btn_start_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (preferences.getBoolean("firstStart", true))
{
L.i(LOGTAG, "扫码");
editor = preferences.edit();
editor.putBoolean("firstStart", false);
editor.apply();
Intent intent1 = new Intent(StartActivity.this, CaptureActivity.class);
startActivityForResult(intent1, REQUEST_CODE);
}
else
{
L.i(LOGTAG, "主页");
Intent intent = new Intent(StartActivity.this,MainPageActivity.class);
startActivity(intent);
finish();
}
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
/** * 处理二维码扫描结果 */
if (requestCode == REQUEST_CODE)
{
//处理扫描结果(在界面上显示)
if (null != data)
{
Bundle bundle = data.getExtras();
if (bundle == null)
{
return;
}
if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS)
{
result = bundle.getString(CodeUtils.RESULT_STRING);
L.i("扫描结果:",result);
}
else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED)
{
Toast.makeText(getApplicationContext(), "解析二维码失败", Toast.LENGTH_SHORT).show();
}
}
}
}
3.添加调用摄像头的权限
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
4.做完以上三步,运行,打开手机的设置权限,允许使用摄像头,成功扫描二维码。
5.我们来看看二维码的原理
//二维码算法
//两个for循环是图片横列扫描的结果
for(int y=0;y<QR_HEIGHT;y++)
{
for(int x;x<QR_WIDTH;x++)
{
if(bitMatri.get(x,y))
{
pixels[y*QR_WIDTH+x]=oxff000000;
}
else
{
pixels[y*QR_WIDTH+x]=oxffffffff;
}
}
}
6.后续
最近又有一个项目是需要用到扫描二维码的功能了,原本想着自己以前用过, command + c , 五分钟之后就应该可以实现的功能,结果发现这代码早过时了~
更新一下新用法:
- build.gradle 添加依赖
dependencies {
implementation 'com.journeyapps:zxing-android-embedded:3.0.2@aar'
implementation 'com.google.zxing:core:3.2.0'
}
- AndroidManifest.xml 添加权限
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
- Activity 中调用
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
@BindView(R.id.btn_scan)
Button btn_scan;
@BindView(R.id.tv_text)
TextView tv_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
btn_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new com.google.zxing.integration.android.IntentIntegrator(MainActivity.this).initiateScan();
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "Scanned");
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
// This is important, otherwise the result will not be passed to the fragment
super.onActivityResult(requestCode, resultCode, data);
}
}
}
-
在 草料二维码生成测试二维码,扫描结果: