Android之ZXing扫描二维码以及生成二维码
ZXIng项目地址:ZXing地址
项目结构
扫描二维码:使用
CaptureActivity类
项目代码:
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;import com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;public class MainActivity extends AppCompatActivity {private TextView results;private EditText editor;private ImageView image;private CheckBox logo;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);results = (TextView) findViewById(R.id.tv_result);editor = (EditText) findViewById(R.id.editor);image = (ImageView) findViewById(R.id.wem_image);logo = (CheckBox) findViewById(R.id.logo);}//扫描二维码
public void scan(View view) {startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class), 0);}@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");results.setText(result);}}//生成二维码
public void make_ewm(View view) {String input = editor.getText().toString();if (input.equals("")) {Toast.makeText(MainActivity.this, "输入不能为空", Toast.LENGTH_SHORT).show();}Bitmap bitmap = EncodingUtils.createQRCode(input, 500, 500, logo.isChecked() ? BitmapFactory.decodeResource(getResources(),android.R.drawable.ic_input_add) : null);image.setImageBitmap(bitmap);}
}
String result = bundle.getString("result");解释::::
二维码生成:使用EncodingUtils.createQRCode方法
Bitmap createQRCode(String content, int widthPix, int heightPix, Bitmap logoBm)
参数说明:
content:二维码输入的内容widthPix:宽度
heightPix:高度
logoBm:中心logo (可以为null)
说明:APP清单文件中不需要打开相机权限:
libzxing:的清单文件中已经做了权限说明:
可能出现的问题:build.gradle需要将minSdkVersion设置在16以上!