1. 程式人生 > >掃描二維碼 生成二維碼

掃描二維碼 生成二維碼

mainactivity.xml佈局

<?xml version="1.0" encoding="utf-8"?>
<Button
    android:id="@+id/btnSan"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="掃描二維碼" />

<TextView
    android:id="@+id/tv_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnSan"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp" />

<EditText
    android:id="@+id/et_input"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tv_content"
    android:layout_marginTop="10dp"
    android:hint="請輸入要生成的二維碼文字" />

<Button
    android:id="@+id/btn_generate"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/et_input"
    android:layout_marginTop="10dp"
    android:text="生成二維碼" />

<ImageView
    android:id="@+id/img"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btn_generate"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="10dp" />

MainActivity頁面

package bw.com.shengcheng;

import android.content.Intent; import android.graphics.Bitmap; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; 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 tv_content; private EditText et_input; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_content=findViewById(R.id.tv_content); et_input=findViewById(R.id.et_input); img=findViewById(R.id.img);

    findViewById(R.id.btnSan).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
  startActivityForResult(new Intent(MainActivity.this, CaptureActivity.class),0);
        }
    });

    findViewById(R.id.btn_generate).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String str=et_input.getText().toString();
            if (str.equals("")){
                Toast.makeText(MainActivity.this,"不能為空",Toast.LENGTH_SHORT).show();;

            }else{
                //點陣圖
                Bitmap bitmap= EncodingUtils.createQRCode(str,500,500,null);
                //設定圖片
                img.setImageBitmap(bitmap);

            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode==RESULT_OK){
        String result=data.getExtras().getString("result");
        Log.d("Main",result);
        tv_content.setText(result);
    }
}

}

導libzxing

over