增加二维码生成/扫描识别示例代码
This commit is contained in:
parent
05f524d5c3
commit
ebada8b871
|
@ -0,0 +1,40 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/white">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/et_qr_text"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:ems="10"
|
||||
android:layout_gravity="left"
|
||||
>
|
||||
</EditText>
|
||||
|
||||
<Button
|
||||
android:id="@+id/btn_make_qr"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="生成二维码"
|
||||
android:layout_gravity="right"
|
||||
/>
|
||||
<Button
|
||||
android:id="@+id/btn_make_bar"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="获取随机验证码"
|
||||
android:layout_gravity="right"
|
||||
/>
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/qr_image"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:minHeight="200dp"
|
||||
android:minWidth="200dp"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
|
@ -0,0 +1,127 @@
|
|||
package com.zftlive.android.sample.zxing;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.zftlive.android.R;
|
||||
import com.zftlive.android.base.BaseActivity;
|
||||
import com.zftlive.android.tools.ToolAlert;
|
||||
import com.zftlive.android.tools.ToolFile;
|
||||
import com.zftlive.android.tools.ToolPicture;
|
||||
|
||||
/**
|
||||
* 生成二维码示例
|
||||
* @author 曾繁添
|
||||
* @version 1.0
|
||||
*
|
||||
*/
|
||||
public class ZxingGenBinActivity extends BaseActivity {
|
||||
|
||||
private EditText et_qr_text;
|
||||
private Button btn_make_qr;
|
||||
private Button btn_make_bar;
|
||||
private ImageView qr_image;
|
||||
private Bitmap qrImage,validateCodeImage;
|
||||
|
||||
@Override
|
||||
public int bindLayout() {
|
||||
return R.layout.activity_gen_qr_image;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initView(View view) {
|
||||
et_qr_text = (EditText)findViewById(R.id.et_qr_text);
|
||||
btn_make_qr = (Button)findViewById(R.id.btn_make_qr);
|
||||
btn_make_bar = (Button)findViewById(R.id.btn_make_bar);
|
||||
qr_image = (ImageView)findViewById(R.id.qr_image);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doBusiness(Context mContext) {
|
||||
//初始化值
|
||||
if("".equals(et_qr_text.getText().toString())){
|
||||
et_qr_text.setText("http://zftlive.qiniudn.com/Android360UI.zip");
|
||||
}
|
||||
|
||||
btn_make_qr.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
try {
|
||||
if("".equals(et_qr_text.getText().toString())){
|
||||
ToolAlert.showShort("请输入要生成二维码内容!");
|
||||
return ;
|
||||
}
|
||||
|
||||
//回收bitmap
|
||||
if(null != qrImage && !qrImage.isRecycled()){
|
||||
qrImage.recycle();
|
||||
qrImage = null;
|
||||
}
|
||||
|
||||
qrImage = ToolPicture.makeQRImage(et_qr_text.getText().toString(), 200, 200);
|
||||
qr_image.setImageBitmap(qrImage);
|
||||
|
||||
//生成图片
|
||||
String filePath = ToolFile.gainSDCardPath() + "/MyLive/QRImage/"+UUID.randomUUID().toString()+".jpg";
|
||||
ToolFile.saveAsJPEG(qrImage, filePath);
|
||||
ToolAlert.showShort("二维码已经保存在:"+filePath);
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
btn_make_bar.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
try {
|
||||
//回收bitmap
|
||||
if(null != validateCodeImage && !validateCodeImage.isRecycled()){
|
||||
validateCodeImage.recycle();
|
||||
validateCodeImage = null;
|
||||
}
|
||||
|
||||
//生成图片
|
||||
validateCodeImage = ToolPicture.makeValidateCode(200, 30);
|
||||
qr_image.setImageBitmap(validateCodeImage);
|
||||
|
||||
ToolAlert.showShort("验证码值:"+ToolPicture.gainValidateCodeValue());
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
//回收bitmap
|
||||
if(null != qrImage && !qrImage.isRecycled()){
|
||||
qrImage.recycle();
|
||||
qrImage = null;
|
||||
}
|
||||
|
||||
if(null != validateCodeImage && !validateCodeImage.isRecycled()){
|
||||
validateCodeImage.recycle();
|
||||
validateCodeImage = null;
|
||||
}
|
||||
|
||||
System.gc();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,43 @@
|
|||
package com.zftlive.android.sample.zxing;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.view.View;
|
||||
|
||||
import com.zftlive.android.base.BaseActivity;
|
||||
import com.zftlive.android.zxing.CaptureActivity;
|
||||
|
||||
/**
|
||||
* 二维码扫描示例代码
|
||||
* @author 曾繁添
|
||||
* @version 1.0
|
||||
*
|
||||
*/
|
||||
public class ZxingSacnnerActivity extends BaseActivity {
|
||||
|
||||
@Override
|
||||
public int bindLayout() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initView(View view) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doBusiness(Context mContext) {
|
||||
Intent intent = new Intent(this,CaptureActivity.class);
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() {
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue