先在androidstudio上导入libzing的Module,然后再app上把那个libzing添加上去,这样我们的app就关联了那个libzing库了
点击libzing下载地址
然后我们就只在app这个工程下写代码就行了。activity_main.xml布局如下
<?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="com.example.edu.myqrcode.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="start scan"
android:onClick="scan"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ruselt"
android:textSize="26sp"/>
<TextView
android:id="@+id/tv_ruselt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="26sp"/>
<EditText
android:id="@+id/et_text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="make"
android:text="make QRCode"/>
<CheckBox
android:id="@+id/is_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="check"/>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
android:layout_gravity="center"/>
</LinearLayout>
Mainactivity.java
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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 com.xys.libzxing.zxing.activity.CaptureActivity;
import com.xys.libzxing.zxing.encoding.EncodingUtils;
public class MainActivity extends Activity {
private TextView mTv_Ruslet;
private EditText mInput;
private ImageView mImg;
private CheckBox isLogo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv_Ruslet= (TextView) findViewById(R.id.tv_ruselt);
mInput= (EditText) findViewById(R.id.et_text);
mImg= (ImageView) findViewById(R.id.img);
isLogo= (CheckBox) findViewById(R.id.is_logo);
}
/**
*生成二维码
*/
public void make(View view){
String input=mInput.getText().toString().trim();
Bitmap bitmap=EncodingUtils.createQRCode(input,500,500,
isLogo.isChecked()? BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher):null
);
mImg.setImageBitmap(bitmap);
}
/**
* 扫描二维码
*/
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");
mTv_Ruslet.setText(result);
} if(resultCode == RESULT_CANCELED) {
mTv_Ruslet.setText("扫描出错");
}
}
}
运行结果:扫描跟生成二维码都是ok的