二维码扫描控件

来源:互联网 发布:使命召唤 知乎 编辑:程序博客网 时间:2024/05/17 08:02

App上面有个二维码现在都是比较普遍了,方便很多更能,而二维码控件的实现也非常方便,所以加上二维码马上可以提高档次。而我使用的就是zxing。


zxing的使用:


            1.导入bulid.gradle:

compile'cn.yipianfengye.android:zxing-library:2.1'



      2.导入xml文件:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_z_xing"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <ImageView        android:id="@+id/iv"        android:layout_width="match_parent"        android:layout_height="300dp" />    <LinearLayout        android:layout_width="match_parent"        android:orientation="horizontal"        android:layout_height="wrap_content">        <Button            android:text="扫描二维码"            android:id="@+id/b1"            android:layout_marginLeft="10dp"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <Button            android:text="扫描图片"            android:id="@+id/b2"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />        <Button            android:text="生成二维码"            android:id="@+id/b3"            android:layout_width="0dp"            android:layout_weight="1"            android:layout_height="wrap_content" />    </LinearLayout>    <EditText        android:layout_marginTop="10dp"        android:id="@+id/ed"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="输入内容"        /></LinearLayout>
  3.导入xml文件:
public class zXingActivity extends AppCompatActivity implements View.OnClickListener {    private Context context;    private int REQUEST_CODE=1;    private int REQUEST_IMAGE=2;    private Button shao;    private Button shaoPhoto;    private Button sheng;    private EditText editText;    private ImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_z_xing);        context=this;        shao=(Button)findViewById(R.id.b1);        shaoPhoto=(Button)findViewById(R.id.b2);        sheng=(Button)findViewById(R.id.b3);        editText=(EditText)findViewById(R.id.ed);        imageView=(ImageView)findViewById(R.id.iv);        shao.setOnClickListener(this);        shaoPhoto.setOnClickListener(this);        sheng.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            //扫描二维码            case R.id.b1:                Intent intent = new Intent(context, CaptureActivity.class);                startActivityForResult(intent,REQUEST_CODE);                break;            //扫描图片            case R.id.b2:                Intent intent1 = new Intent(Intent.ACTION_GET_CONTENT);                intent1.addCategory(Intent.CATEGORY_OPENABLE);                intent1.setType("image/*");                startActivityForResult(intent1, REQUEST_IMAGE);                break;            //生成二维码            case R.id.b3:                Bitmap mBitmap ;                String textContent = editText.getText().toString();                if (TextUtils.isEmpty(textContent)) {                    Toast.makeText(context, "您的输入为空!", Toast.LENGTH_SHORT).show();                    return;                }                editText.setText("");                mBitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));                imageView.setImageBitmap(mBitmap);                break;        }    }    //Intent返回会执行这个方法    protected void onActivityResult(int requestCode, int resultCode, 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) {                    String result = bundle.getString(CodeUtils.RESULT_STRING);                    Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show();                } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {                    Toast.makeText(context, "解析二维码失败", Toast.LENGTH_LONG).show();                }            }        }        if (requestCode == REQUEST_IMAGE) {            if (data != null) {                Uri uri = data.getData();                ContentResolver cr = getContentResolver();                try {                    Bitmap mBitmap = MediaStore.Images.Media.getBitmap(cr, uri);//显得到bitmap图片                    CodeUtils.analyzeBitmap(mBitmap.toString(), new CodeUtils.AnalyzeCallback() {                        @Override                        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {                            Toast.makeText(context, "解析结果:" + result, Toast.LENGTH_LONG).show();                        }                        @Override                        public void onAnalyzeFailed() {                            Toast.makeText(context, "解析二维码失败", Toast.LENGTH_LONG).show();                        }                    });                    if (mBitmap != null) {                        mBitmap.recycle();                    }                } catch (Exception e) {                    e.printStackTrace();                }            }        }    }}
非常方便的集成了二维码的使用,在App的ActivityBar上面加个控件点击跳转扫面二维码是非常好的一个布局,可以尝试使用zxing来实现二维码的扫面。