CustomCaptrueActivity、、、
package com.dash.a06_qr_code;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import com.dash.zxinglibrary.activity.CaptureActivity;
import com.dash.zxinglibrary.activity.CaptureFragment;
import com.dash.zxinglibrary.activity.CodeUtils;
public class CustomCaptrueActivity 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_custom_captrue);
//使用扫描的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;
}
}
});
}
}
MainActivity
package com.dash.a06_qr_code;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.dash.zxinglibrary.activity.CaptureActivity;
import com.dash.zxinglibrary.activity.CodeUtils;
/**
* 1.初始化库文件..........
*/
public class MainActivity extends AppCompatActivity {
private final int REQUEST_CODE = 1001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* 默认扫描界面
* @param view
*/
public void defaultScan(View view) {
Intent intent = new Intent(MainActivity.this, CaptureActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
/**
* 自定义的扫描页面
* @param view
*/
public void dingZhiScan(View view) {
Intent intent = new Intent(MainActivity.this, CustomCaptrueActivity.class);
startActivityForResult(intent, REQUEST_CODE);
}
public void picScan(View view) {
}
/**
* 生成
* @param view
*/
public void shengCheng(View view) {
Intent intent = new Intent(MainActivity.this, ShengChengActivity.class);
startActivity(intent);
}
/**
* 接收扫描的结果
* @param requestCode
* @param resultCode
* @param data
*/
@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();
}
}
}
}
}
MyApplication
package com.dash.a06_qr_code;
import android.app.Application;
import com.dash.zxinglibrary.activity.ZXingLibrary;
/**
* Created by Dash on 2018/1/2.
*/
public class MyApplication extends Application{
@Override
public void onCreate() {
super.onCreate();
//初始化zxing库
ZXingLibrary.initDisplayOpinion(this);
}
}
ShengChengActivity
package com.dash.a06_qr_code;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import com.dash.zxinglibrary.activity.CodeUtils;
public class ShengChengActivity extends AppCompatActivity {
private EditText editText;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheng_cheng);
editText = findViewById(R.id.edit_text_01);
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.mipmap.ic_launcher));
imageView.setImageBitmap(bitmap);
}
}
布局
<?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.dash.a06_qr_code.CustomCaptrueActivity">
<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>
Main
<?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"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dash.a06_qr_code.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>
shengcheng
<?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"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>
mycammer
<?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"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/preview_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!--取景框-->
<com.dash.zxinglibrary.view.ViewfinderView
android:id="@+id/viewfinder_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:inner_corner_color="@color/scan_corner_color"
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>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dash.a06_qr_code">
<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" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 扫描的页面 -->
<activity android:name="com.dash.zxinglibrary.activity.CaptureActivity" />
<activity android:name=".CustomCaptrueActivity"></activity>
<activity android:name=".ShengChengActivity"/>
</application>
</manifest>
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.dash.a06_qr_code"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation project(':zxinglibrary')
}