package com.example.lenovo.lx3.activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.lenovo.lx3.R;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
public class Main2Activity extends AppCompatActivity {
private EditText ed;
private Button sao;
private Button grow;
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ed =findViewById(R.id.ed);
sao =findViewById(R.id.btn_sao);
grow =findViewById(R.id.btn_grow);
image =findViewById(R.id.image);
sao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Main2Activity.this, CaptureActivity.class);
startActivityForResult(intent, 101);
}
});
grow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String textContent = ed.getText().toString();
if (TextUtils.isEmpty(textContent)) {
Toast.makeText(Main2Activity.this, "您的输入为空!", Toast.LENGTH_SHORT).show();
return;
}
ed.setText("");
final Bitmap bitmap = CodeUtils.createImage(textContent, 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
image.setImageBitmap(bitmap);
}
});
CodeUtils.isLightEnable(true);//打开闪光灯
CodeUtils.isLightEnable(false);//关闭闪光灯
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101) {
//处理扫描结果(在界面上显示)
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(Main2Activity.this, "解析二维码失败", Toast.LENGTH_LONG).show();
}
}
}
}
}