依赖
implementation 'cn.bingoogolapple:bga-qrcode-zxing:1.3.4'
Fragment
public class FragmentMy extends Fragment{
private ImageView imageView;
private Button search_button;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.my,container,false);
imageView=view.findViewById(R.id.imageView);
search_button=view.findViewById(R.id.search_button);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
inif();
search_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(getActivity(),MyScanActivity.class);
startActivity(intent);
}
});
}
private void inif() {
String name = ((SuccessActivity) getActivity()).setName();
QRText qrText=new QRText(getActivity(),imageView,name);
qrText.execute(name);
}
class QRText extends AsyncTask<String,Void,Bitmap>{
private WeakReference<ImageView> mimageView ;
private WeakReference<Context> mcontext;
public QRText(Context context,ImageView imageView,String name){
mcontext=new WeakReference<>(context);
mimageView=new WeakReference<>(imageView);
}
@Override
protected Bitmap doInBackground(String... strings) {
String s = strings[0];
if (TextUtils.isEmpty(s)){
return null;
}
int size = mcontext.get().getResources().getDimensionPixelOffset(R.dimen.ss);
return QRCodeEncoder.syncEncodeQRCode(s,size);
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap!=null){
mimageView.get().setImageBitmap(bitmap);
}else{
Toast.makeText(mcontext.get(),"失败",Toast.LENGTH_SHORT).show();
}
}
}
}
跳转扫一扫
public class ScanActivity extends AppCompatActivity implements QRCodeView.Delegate {
private ZXingView mZXingView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scan);
mZXingView = findViewById(R.id.zxingview);
mZXingView.setDelegate(this);
}
@Override
protected void onResume() {
super.onResume();
mZXingView.startCamera();
mZXingView.startSpotAndShowRect();
}
@Override
protected void onStop() {
super.onStop();
mZXingView.stopCamera();
}
@Override
protected void onDestroy() {
super.onDestroy();
mZXingView.onDestroy();
Toast.makeText(ScanActivity.this,11+"",Toast.LENGTH_SHORT).show();
}
@Override
public void onScanQRCodeSuccess(String result) {
if(result!=null){
Intent intent=new Intent(ScanActivity.this,ContentActivity.class);
intent.putExtra("result",result);
startActivity(intent);
}
Toast.makeText(ScanActivity.this,result,Toast.LENGTH_SHORT).show();
}
@Override
public void onCameraAmbientBrightnessChanged(boolean isDark) {
}
@Override
public void onScanQRCodeOpenCameraError() {
}
布局
<cn.bingoogolapple.qrcode.zxing.ZXingView
android:id="@+id/zxingview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Intent intent1=getIntent();
String name = intent1.getStringExtra("name");
getname(name);
}
public static String getname(String name){
return name;
}
}