MainActivity:
public class MainActivity extends AppCompatActivity { private static final int REQUEST_CODE =1001 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //默认页面扫描 public void defaultScan(View view) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent, REQUEST_CODE); } //定制页面扫描 public void dingZhiScan(View view) { } //选择图片解析 public void picScan(View view) { } public void shengCheng(View view) { Intent intent = new Intent(MainActivity.this, Shengcheng.class); startActivity(intent); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { //处理扫描结果(在界面上显示) 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(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show(); } } } } }
CaptureActivity:
public class CaptureActivity extends AppCompatActivity { /** * 二维码解析回调函数 */ CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() { //解析成功的回调 @Override public void onAnalyzeSuccess(Bitmap mBitmap, String result) { Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS); bundle.putString(CodeUtils.RESULT_STRING, result); resultIntent.putExtras(bundle); setResult(RESULT_OK, resultIntent); finish(); } //解析失败的回调 @Override public void onAnalyzeFailed() { Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED); bundle.putString(CodeUtils.RESULT_STRING, ""); resultIntent.putExtras(bundle); setResult(RESULT_OK, resultIntent); finish(); } }; private boolean flag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture); //使用扫描的fragment替换frameLayout布局....captureActivity真正扫描的操作交给fragment //扫描成功或者失败的监听应该设置给fragment CaptureFragment captureFragment = new CaptureFragment(); //设置自定义的...扫描布局 //给扫描的fragment定制一个页面 CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera); //设置监听 captureFragment.setAnalyzeCallback(analyzeCallback); getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container,captureFragment).commit(); flag = false; //闪光灯 findViewById(R.id.linear1).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (flag){//关闭 //打开 CodeUtils.isLightEnable(false); flag = false; }else { //打开 CodeUtils.isLightEnable(true); flag = true; } } }); } }Shengcheng:
public class Shengcheng extends AppCompatActivity { private EditText editText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_shengcheng); editText =(EditText) findViewById(R.id.edit_text_01); imageView =(ImageView)findViewById(R.id.image_view); } /** * 普通的 * @param view */ public void normal(View view) { String s = editText.getText().toString(); if (s==""){ return; } Bitmap bitmap = CodeUtils.createImage(s, 300, 300, null); imageView.setImageBitmap(bitmap); } public void logoQR(View view) { String s = editText.getText().toString(); if (s==""){ return; } //logo Bitmap bitmap = CodeUtils.createImage(s, 300, 300, BitmapFactory.decodeResource(getResources(),R.drawable.a)); imageView.setImageBitmap(bitmap); } }MainActivity的布局:<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.erweima_dome01.MainActivity"> <Button android:text="默认页面扫描" android:onClick="defaultScan" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="定制页面扫描" android:onClick="dingZhiScan" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="选择图片解析" android:onClick="picScan" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:text="生成" android:onClick="shengCheng" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>CaptureActivity的布局(里面有两个布局):
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" tools:context="com.example.erweima_dome01.CaptureActivity"> <FrameLayout android:id="@+id/fl_my_container" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:background="#AA333333" android:orientation="horizontal" android:paddingBottom="15dp" android:paddingTop="15dp"> <LinearLayout android:id="@+id/linear1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="闪光灯" android:textColor="#FFFFFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="封面" android:textColor="#FFFFFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="街景" android:textColor="#FFFFFF" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@mipmap/ic_launcher" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="翻译" android:textColor="#FFFFFF" /> </LinearLayout> </LinearLayout> </FrameLayout>CaptureActivity中另一个布局
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"> <SurfaceView android:id="@+id/preview_view" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!--取景框--> <com.uuzuche.lib_zxing.view.ViewfinderView android:id="@+id/viewfinder_view" android:layout_width="wrap_content" android:layout_height="wrap_content" app:inner_corner_color="#ff0000" app:inner_corner_length="30dp" app:inner_corner_width="5dp" app:inner_height="200dp" app:inner_margintop="150dp" app:inner_scan_bitmap="@drawable/scan_image" app:inner_scan_iscircle="true" app:inner_scan_speed="10" app:inner_width="200dp" /> </FrameLayout>Shengcheng的布局
<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.erweima_dome01.Shengcheng"> <EditText android:id="@+id/edit_text_01" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="normal" android:text="普通的二维码" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:onClick="logoQR" android:text="logo二维码" android:layout_width="match_parent" android:layout_height="wrap_content" /> <ImageView android:id="@+id/image_view" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>别忘了权限
二维码的依赖:<uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" />compile 'cn.yipianfengye.android:zxing-library:2.1'