获取按钮:
Button btn_scan_view=(Button) findViewById(R.id.button_get);
按钮点击事件:
//点击开始生成按钮监听事件
btn_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击事件内容
}
});
输入框:
EditText edittext=findViewById(R.id.editTextText);//输入框
获取输入内容:
String input= edittext.getText().toString();
获取图像控件:
ImageView img_view=(ImageView)findViewById(R.id.imageView);
将图像填充到控件:
img_view.setImageBitmap();
使用Intent来实现页面跳转
首先,我们需要创建一个新的Android Activity。在Android Studio中,可以通过以下步骤实现:
在项目的结构视图中,右键点击app -> New -> Activity -> Blank Activity,然后给新的Activity命名并选择放在哪个文件夹下。
在AndroidManifest.xml文件中,我们需要为新的Activity注册一个类别。
//监听按钮,如果点击,就跳转
ntent intent = new Intent();
//前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面
intent.setClass(MainActivity.this,ScanActivity.class);//ScanActivity修改为待跳转页面
startActivity(intent);
创建项目修改镜像提速(settings.gradle.kts)文件修改为:
pluginManagement {
repositories {
maven { setUrl("https://maven.aliyun.com/repository/public") }
maven { setUrl("https://maven.aliyun.com/repository/central") }
google()
mavenCentral()
gradlePluginPortal()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
maven { setUrl("https://maven.aliyun.com/repository/public") }
maven { setUrl("https://maven.aliyun.com/repository/central") }
google()
mavenCentral()
}
}
maven添加jar包,在build.gradle.kts添加:
implementation("com.journeyapps:zxing-android-embedded:3.6.0")
代码:
package com.example.week08_or;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import java.util.Hashtable;
public class MainActivity extends AppCompatActivity {
Button btn_get;
Button btn_scan_view;
ImageView img_view;
EditText edittext;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get=(Button) findViewById(R.id.button_get);
img_view=(ImageView)findViewById(R.id.imageView);
btn_scan_view=(Button)findViewById(R.id.button_page);//跳转页面
edittext =findViewById(R.id.editTextText);//输入框
//点击开始生成按钮监听事件
btn_get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input= edittext.getText().toString();
//调用CreateUtil类生成二维码后显示在界面上
img_view.setImageBitmap(createQRCode(input,img_view.getWidth(),img_view.getHeight()));
}
});
btn_scan_view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//监听按钮,如果点击,就跳转
Intent intent = new Intent();
//前一个(MainActivity.this)是目前页面,后面一个是要跳转的下一个页面
intent.setClass(MainActivity.this,ScanActivity.class);
startActivity(intent);
}
});
}
//将输入的字符串转为图像
public Bitmap createQRCode(String codestring, int width, int height){
try {
//首先判断参数的合法性,要求字符串内容不能为空或图片长宽必须大于0
if (TextUtils.isEmpty(codestring)||width<=0||height<=0){
return null;
}
//设置二维码的相关参数,生成BitMatrix(位矩阵)对象
Hashtable hashtable=new Hashtable<>();
hashtable.put(EncodeHintType.CHARACTER_SET,"utf-8"); //设置字符转码格式
hashtable.put(EncodeHintType.ERROR_CORRECTION,"H"); //设置容错级别
hashtable.put(EncodeHintType.MARGIN,"2"); //设置空白边距
//encode需要抛出和处理异常
BitMatrix bitMatrix=new QRCodeWriter().encode(codestring, BarcodeFormat.QR_CODE,width,height,hashtable);
//再创建像素数组,并根据位矩阵为数组元素赋颜色值
int[] pixel=new int[width*width];
for (int h=0;h
layout文件:
代码:
package com.example.week08_or;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
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 com.journeyapps.barcodescanner.CaptureActivity;
public class ScanActivity extends AppCompatActivity {
Button btn_scan;
Button btn_back;
TextView textView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);//绑定页面
btn_scan=(Button)findViewById(R.id.btn_scan);
btn_back=(Button) findViewById(R.id.button2);
textView=(TextView)findViewById(R.id.textView);
//扫描二维码按钮点击事件
btn_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//.QR_CODE_TYPES 二维码
//ONE_D_CODE_TYPES条形码
//ALL_CODE_TYPES 所有类型
new IntentIntegrator(ScanActivity.this).setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)// 扫码的类型,可选:一维码,二维码,一/二维码
.setPrompt("请对准二维码")// 设置提示语
.setCameraId(0)// 选择摄像头,可使用前置或者后置
.setBeepEnabled(false)// 是否开启声音,扫完码之后会"哔"的一声
//.setBarcodeImageEnabled(true)// 扫完码之后生成二维码的图片
.setCaptureActivity(CaptureActivity.class)
.initiateScan();// 初始化扫码
}
});
//返回上一页面
btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
#回调函数,处理扫描结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if(result != null) {
if(result.getContents() == null) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
textView.setText(result.getContents());
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
布局文件:
需要将
setCaptureActivity(CaptureActivity.class)
修改为:
setCaptureActivity(CustomCaptureActivity.class)
//CustomCaptureActivity为自定义activity名称。
Activity:
package com.example.week08_or;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.KeyEvent;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.journeyapps.barcodescanner.CaptureManager;
import com.journeyapps.barcodescanner.DecoratedBarcodeView;
public class CustomCaptureActivity extends AppCompatActivity {
private CaptureManager capture;
private DecoratedBarcodeView barcodeScannerView;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_capture);// 自定义布局
barcodeScannerView = (DecoratedBarcodeView) findViewById(R.id.dbv_custom);
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
}
layout: