扫描二维码和生成二维码
导入依赖包libzxing
MainActivity:
package com.example.ass.myapplication2;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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 AppCompatActivity {
private static final String TAG = "MainActivity";
private final int REQUESTCODE = 0;//请求码
private final int RESULTCODE = 1;//结果码
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
Button create = (Button) findViewById(R.id.create);
final EditText content = (EditText) findViewById(R.id.content);
final ImageView bit = (ImageView) findViewById(R.id.bit);
textView = (TextView) findViewById(R.id.result_0);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView.setText("");
Intent intent = new Intent(MainActivity.this,CaptureActivity.class);
startActivityForResult(intent,REQUESTCODE);
}
});
create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String content_string = content.getText().toString();
Log.d(TAG, "onClick() returned: " + "取到的内容---" + content_string);
Bitmap bitmap = EncodingUtils.createQRCode(content_string, 200, 200, null);
bit.setImageBitmap(bitmap);
}
});
}
//接收返回结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult() returned: " + "接收结果.....");
if (resultCode==RESULT_OK){
Log.d(TAG, "onActivityResult() returned: " + "返回成功");
Bundle bundle = data.getExtras();
String result = bundle.getString("result");
textView.setText(result);
Log.d(TAG,"----"+result+"---");
}
}
}
布局
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.ass.myapplication2.MainActivity"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始扫描" /> <TextView android:id="@+id/result" android:text="扫描结果是:" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/result_0" android:text="" android:textSize="35sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/create" android:text="生成二维码" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/bit" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
one_activity.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/start" android:text="开始扫描" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/img0" android:src="@drawable/aa" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ImageView android:id="@+id/img1" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>