二维码的解析和二维码生成

时间:2023-02-05 11:13:23
扫码使很多应用都有的一个功能,公司刚好需要一个扫码功能就写了个Demo,希望对需要的人有用。先上图: 二维码的解析和二维码生成
二维码的解析和二维码生成
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="littlestory.com.demo.MainActivity">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="scan"
android:text="Start Scan" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Result:" />
<TextView
android:id="@+id/tv_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<EditText
android:id="@+id/tv_text"
android:hint="Input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Make QRCode"
android:onClick="make"/>
<CheckBox
android:id="@+id/cb_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Logo"/>
<ImageView
android:id="@+id/iv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@mipmap/ic_launcher"/>
</LinearLayout>
 
 
public class MainActivity extends Activity {    private TextView mTvResult;    private EditText mInput;    private ImageView mResult;    private CheckBox mLogo;    @Override protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mTvResult= (TextView) findViewById(R.id.tv_result);        mInput= (EditText) findViewById(R.id.tv_text);        mResult= (ImageView) findViewById(R.id.iv_result);        mLogo= (CheckBox) findViewById(R.id.cb_logo);    }    public void scan(View view){        startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class),0);    }    public void make(View view){        String input=mInput.getText().toString();        if(input.equals("")){            Toast.makeText(MainActivity.this,"输入不能为空",Toast.LENGTH_LONG).show();        }else {            Bitmap bitmap= EncodingUtils.createQRCode(input,500,500,mLogo.isChecked()?                    BitmapFactory.decodeResource(getResources(),R.mipmap.b):null);            mResult.setImageBitmap(bitmap);        }    }    @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if(resultCode==RESULT_OK){            Bundle bundle=data.getExtras();            String result=bundle.getString("result");            mTvResult.setText(result);        }    }}