Repository: yangxch/GenerateQRCode Branch: master Commit: 7e545f12840d Files: 39 Total size: 76.5 KB Directory structure: gitextract_h08b587n/ ├── .gitignore ├── .idea/ │ ├── codeStyles/ │ │ └── Project.xml │ ├── compiler.xml │ ├── jarRepositories.xml │ ├── misc.xml │ ├── modules.xml │ ├── runConfigurations.xml │ └── vcs.xml ├── LICENSE ├── README.md ├── app/ │ ├── .gitignore │ ├── build.gradle │ ├── libs/ │ │ └── core-3.3.0.jar │ ├── proguard-rules.pro │ ├── release/ │ │ └── output.json │ └── src/ │ ├── androidTest/ │ │ └── java/ │ │ └── com/ │ │ └── example/ │ │ └── xch/ │ │ └── generateqrcode/ │ │ └── ExampleInstrumentedTest.java │ ├── main/ │ │ ├── AndroidManifest.xml │ │ ├── java/ │ │ │ └── com/ │ │ │ └── example/ │ │ │ └── xch/ │ │ │ └── generateqrcode/ │ │ │ ├── ImageUtil.java │ │ │ ├── MainActivity.java │ │ │ └── QRCodeUtil.java │ │ └── res/ │ │ ├── drawable/ │ │ │ ├── ic_launcher_background.xml │ │ │ └── list_bg.xml │ │ ├── drawable-v24/ │ │ │ └── ic_launcher_foreground.xml │ │ ├── layout/ │ │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26/ │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── values/ │ │ │ ├── array.xml │ │ │ ├── colors.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ │ └── xml/ │ │ └── file_paths.xml │ └── test/ │ └── java/ │ └── com/ │ └── example/ │ └── xch/ │ └── generateqrcode/ │ └── ExampleUnitTest.java ├── build.gradle ├── gradle/ │ └── wrapper/ │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Built application files *.apk *.ap_ # Files for the ART/Dalvik VM *.dex # Java class files *.class # Generated files bin/ gen/ out/ # Gradle files .gradle/ build/ # Local configuration file (sdk path, etc) local.properties # Proguard folder generated by Eclipse proguard/ # Log Files *.log # Android Studio Navigation editor temp files .navigation/ # Android Studio captures folder captures/ # IntelliJ *.iml .idea/workspace.xml .idea/tasks.xml .idea/gradle.xml .idea/assetWizardSettings.xml .idea/dictionaries .idea/libraries .idea/caches # Keystore files # Uncomment the following line if you do not want to check your keystore files in. #*.jks # External native build folder generated in Android Studio 2.2 and later .externalNativeBuild # Google Services (e.g. APIs or Firebase) google-services.json # Freeline freeline.py freeline/ freeline_project_description.json # fastlane fastlane/report.xml fastlane/Preview.html fastlane/screenshots fastlane/test_output fastlane/readme.md ================================================ FILE: .idea/codeStyles/Project.xml ================================================ ================================================ FILE: .idea/compiler.xml ================================================ ================================================ FILE: .idea/jarRepositories.xml ================================================ ================================================ FILE: .idea/misc.xml ================================================ ================================================ FILE: .idea/modules.xml ================================================ ================================================ FILE: .idea/runConfigurations.xml ================================================ ================================================ FILE: .idea/vcs.xml ================================================ ================================================ FILE: LICENSE ================================================ Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ================================================ FILE: README.md ================================================ # GenerateQRCode 自定义二维码生成:一般二维码、彩色二维码、带logo二维码或带logo彩色二维码、黑色色块用图片代替的二维码 ## 效果图 一般二维码


彩色二维码
带logo二维码
带logo彩色二维码
黑色色块用图片代替的二维码
黑色色块用图片代替且带logo

### 动态更换logo和黑色色块 增加拍照或从相册选取图片功能





### 二维码保存和分享
*** 更多技术干货,欢迎关注我的公众号:程序员大佬超
================================================ FILE: app/.gitignore ================================================ /build ================================================ FILE: app/build.gradle ================================================ apply plugin: 'com.android.application' android { compileSdkVersion 27 defaultConfig { applicationId "com.example.xch.generateqrcode" minSdkVersion 15 targetSdkVersion 27 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(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:27.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' // implementation 'com.google.zxing:core:3.3.0' } ================================================ FILE: app/proguard-rules.pro ================================================ # Add project specific ProGuard rules here. # You can control the set of applied configuration files using the # proguardFiles setting in build.gradle. # # For more details, see # http://developer.android.com/guide/developing/tools/proguard.html # If your project uses WebView with JS, uncomment the following # and specify the fully qualified class name to the JavaScript interface # class: #-keepclassmembers class fqcn.of.javascript.interface.for.webview { # public *; #} # Uncomment this to preserve the line number information for # debugging stack traces. #-keepattributes SourceFile,LineNumberTable # If you keep the line number information, uncomment this to # hide the original source file name. #-renamesourcefileattribute SourceFile ================================================ FILE: app/release/output.json ================================================ [{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] ================================================ FILE: app/src/androidTest/java/com/example/xch/generateqrcode/ExampleInstrumentedTest.java ================================================ package com.example.xch.generateqrcode; import android.content.Context; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; import static org.junit.Assert.*; /** * Instrumented test, which will execute on an Android device. * * @see Testing documentation */ @RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { @Test public void useAppContext() { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.example.xch.generateqrcode", appContext.getPackageName()); } } ================================================ FILE: app/src/main/AndroidManifest.xml ================================================ ================================================ FILE: app/src/main/java/com/example/xch/generateqrcode/ImageUtil.java ================================================ package com.example.xch.generateqrcode; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public class ImageUtil { /** * 保存图片到指定路径 * * @param context * @param bitmap 要保存的图片 * @param fileName 自定义图片名称 * @return */ public static boolean saveImageToGallery(Context context, Bitmap bitmap, String fileName) { // 保存图片至指定路径 String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "qrcode"; File appDir = new File(storePath); if (!appDir.exists()) { appDir.mkdir(); } File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); //通过io流的方式来压缩保存图片(80代表压缩20%) boolean isSuccess = bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos); fos.flush(); fos.close(); //发送广播通知系统图库刷新数据 Uri uri = Uri.fromFile(file); context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); if (isSuccess) { return true; } else { return false; } } catch (IOException e) { e.printStackTrace(); } return false; } } ================================================ FILE: app/src/main/java/com/example/xch/generateqrcode/MainActivity.java ================================================ package com.example.xch.generateqrcode; import android.Manifest; import android.annotation.SuppressLint; import android.content.ContentUris; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.net.Uri; import android.os.Build; import android.provider.DocumentsContract; import android.provider.MediaStore; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v4.content.FileProvider; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toast; import java.io.File; import java.io.IOException; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button btn_generate; private EditText et_content; private ImageView iv_qrcode; private ImageView picture_logo, picture_black;//logo,代替黑色色块的图片 private EditText et_width, et_height; private Spinner sp_error_correction_level, sp_margin, sp_color_black, sp_color_white; private String content;//二维码内容 private int width, height;//宽度,高度 private String error_correction_level, margin;//容错率,空白边距 private int color_black, color_white;//黑色色块,白色色块 public static final int TAKE_PHOTO = 1;//拍照 public static final int CHOOSE_PHOTO = 2;//从相册选择图片 private Uri imageUri; private Bitmap logoBitmap;//logo图片 private Bitmap blackBitmap;//代替黑色色块的图片 private int remark;//标记返回的是logo还是代替黑色色块图片 private Bitmap qrcode_bitmap;//生成的二维码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_content = findViewById(R.id.et_content); btn_generate = findViewById(R.id.btn_generate); iv_qrcode = findViewById(R.id.iv_qrcode); picture_logo = findViewById(R.id.picture_logo); picture_black = findViewById(R.id.picture_black); et_width = findViewById(R.id.et_width); et_height = findViewById(R.id.et_height); sp_error_correction_level = findViewById(R.id.sp_error_correction_level); sp_margin = findViewById(R.id.sp_margin); sp_color_black = findViewById(R.id.sp_color_black); sp_color_white = findViewById(R.id.sp_color_white); btn_generate.setOnClickListener(this); picture_logo.setOnClickListener(this); picture_black.setOnClickListener(this); iv_qrcode.setOnLongClickListener(new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { imgChooseDialog(); return true; } }); sp_error_correction_level.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { error_correction_level = getResources().getStringArray(R.array.spinarr_error_correction)[position]; } @Override public void onNothingSelected(AdapterView parent) { } }); sp_margin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { margin = getResources().getStringArray(R.array.spinarr_margin)[position]; } @Override public void onNothingSelected(AdapterView parent) { } }); sp_color_black.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String str_color_black = getResources().getStringArray(R.array.spinarr_color_black)[position]; if (str_color_black.equals("黑色")) { color_black = Color.BLACK; } else if (str_color_black.equals("白色")) { color_black = Color.WHITE; } else if (str_color_black.equals("蓝色")) { color_black = Color.BLUE; } else if (str_color_black.equals("绿色")) { color_black = Color.GREEN; } else if (str_color_black.equals("黄色")) { color_black = Color.YELLOW; } else if (str_color_black.equals("红色")) { color_black = Color.RED; } else if (str_color_black.equals("紫色")) { color_black = Color.parseColor("#9370DB"); } else if (str_color_black.equals("粉红色")) { color_black = Color.parseColor("#ffc0cb"); } else if (str_color_black.equals("薄荷色")) { color_black = Color.parseColor("#BDFCC9"); } else { color_black = Color.BLACK; } } @Override public void onNothingSelected(AdapterView parent) { } }); sp_color_white.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { String str_color_white = getResources().getStringArray(R.array.spinarr_color_white)[position]; if (str_color_white.equals("黑色")) { color_white = Color.BLACK; } else if (str_color_white.equals("白色")) { color_white = Color.WHITE; } else if (str_color_white.equals("蓝色")) { color_white = Color.BLUE; } else if (str_color_white.equals("绿色")) { color_white = Color.GREEN; } else if (str_color_white.equals("黄色")) { color_white = Color.YELLOW; } else if (str_color_white.equals("红色")) { color_white = Color.RED; } else if (str_color_white.equals("紫色")) { color_white = Color.parseColor("#9370DB"); } else if (str_color_white.equals("粉红色")) { color_white = Color.parseColor("#ffc0cb"); } else if (str_color_white.equals("薄荷色")) { color_white = Color.parseColor("#BDFCC9"); } else { color_white = Color.WHITE; } } @Override public void onNothingSelected(AdapterView parent) { } }); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_generate://生成 generateQrcodeAndDisplay(); break; case R.id.picture_logo://选取logo remark = 0; showChooseDialog(); break; case R.id.picture_black://选取代替黑色色块的图片 remark = 1; showChooseDialog(); break; default: break; } } /** * 保存图片至本地 * @param bitmap */ private void saveImg(Bitmap bitmap){ String fileName = "qr_"+System.currentTimeMillis() + ".jpg"; boolean isSaveSuccess = ImageUtil.saveImageToGallery(MainActivity.this, bitmap,fileName); if (isSaveSuccess) { Toast.makeText(MainActivity.this, "图片已保存至本地", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "保存图片失败,请稍后重试", Toast.LENGTH_SHORT).show(); } } /** * 分享图片(直接将bitamp转换为Uri) * @param bitmap */ private void shareImg(Bitmap bitmap){ Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null)); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*");//设置分享内容的类型 intent.putExtra(Intent.EXTRA_STREAM, uri); intent = Intent.createChooser(intent, "分享"); startActivity(intent); } /** * 生成二维码并显示 */ private void generateQrcodeAndDisplay() { content = et_content.getText().toString(); String str_width = et_width.getText().toString(); String str_height = et_height.getText().toString(); if (str_width.length() <= 0 || str_height.length() <= 0) { width = 650; height = 650; } else { width = Integer.parseInt(str_width); height = Integer.parseInt(str_height); } if (content.length() <= 0) { Toast.makeText(this, "你没有输入二维码内容哟!", Toast.LENGTH_SHORT).show(); return; } qrcode_bitmap = QRCodeUtil.createQRCodeBitmap(content, width, height, "UTF-8", error_correction_level, margin, color_black, color_white, logoBitmap, 0.2F, blackBitmap); iv_qrcode.setImageBitmap(qrcode_bitmap); } /** * 弹出选择框(拍照或从相册选取图片) * * @author xch */ private void showChooseDialog() { AlertDialog.Builder choiceBuilder = new AlertDialog.Builder(this); choiceBuilder.setCancelable(false); choiceBuilder .setTitle("选择图片") .setSingleChoiceItems(new String[]{"拍照上传", "从相册选择"}, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0://拍照 takePhoto(); break; case 1:// 从相册选择 choosePhotoFromAlbum(); break; default: break; } dialog.dismiss(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); choiceBuilder.create(); choiceBuilder.show(); } /** * 长按二维码图片弹出选择框(保存或分享) */ private void imgChooseDialog(){ AlertDialog.Builder choiceBuilder = new AlertDialog.Builder(MainActivity.this); choiceBuilder.setCancelable(false); choiceBuilder .setTitle("选择") .setSingleChoiceItems(new String[]{"存储至手机", "分享"}, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0://存储 saveImg(qrcode_bitmap); break; case 1:// 分享 shareImg(qrcode_bitmap); break; default: break; } dialog.dismiss(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { } }); choiceBuilder.create(); choiceBuilder.show(); } /** * 拍照 */ private void takePhoto() { // 创建File对象,用于存储拍照后的图片 File outputImage = new File(getExternalCacheDir(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } if (Build.VERSION.SDK_INT < 24) { imageUri = Uri.fromFile(outputImage); } else { imageUri = FileProvider.getUriForFile(MainActivity.this, "com.example.xch.generateqrcode.fileprovider", outputImage); } // 启动相机程序 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PHOTO); } /** * 从相册选取图片 */ private void choosePhotoFromAlbum() { if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { openAlbum(); } } /** * /打开相册 */ private void openAlbum() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); } @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openAlbum(); } else { Toast.makeText(this, "你拒绝了权限申请,可能无法打开相册哟", Toast.LENGTH_SHORT).show(); } break; default: } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case TAKE_PHOTO: if (resultCode == RESULT_OK) { try { if (remark == 0) {//logo logoBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); // 将拍摄的照片显示出来 picture_logo.setImageBitmap(logoBitmap); } else if (remark == 1) {//black blackBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); picture_black.setImageBitmap(blackBitmap); } else { } } catch (Exception e) { e.printStackTrace(); } } break; case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { // 判断手机系统版本号 if (Build.VERSION.SDK_INT >= 19) { // 4.4及以上系统使用这个方法处理图片 handleImageOnKitKat(data); } else { // 4.4以下系统使用这个方法处理图片 handleImageBeforeKitKat(data); } } break; default: break; } } /** * 4.4以后 * * @param data */ @SuppressLint("NewApi") private void handleImageOnKitKat(Intent data) { String imagePath = null; Uri uri = data.getData(); Log.d("TAG", "handleImageOnKitKat: uri is " + uri); if (DocumentsContract.isDocumentUri(this, uri)) { // 如果是document类型的Uri,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; // 解析出数字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { // 如果是content类型的Uri,则使用普通方式处理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { // 如果是file类型的Uri,直接获取图片路径即可 imagePath = uri.getPath(); } displayImage(imagePath); // 根据图片路径显示图片 } /** * 4.4版本以前,直接获取真实路径 * * @param data */ private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); } private String getImagePath(Uri uri, String selection) { String path = null; // 通过Uri和selection来获取真实的图片路径 Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null) { if (cursor.moveToFirst()) { path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; } /** * 显示图片 * * @param imagePath 图片路径 */ private void displayImage(String imagePath) { if (imagePath != null) { if (remark == 0) {//logo logoBitmap = BitmapFactory.decodeFile(imagePath); // 显示图片 picture_logo.setImageBitmap(logoBitmap); } else if (remark == 1) {//black blackBitmap = BitmapFactory.decodeFile(imagePath); picture_black.setImageBitmap(blackBitmap); } else { } } else { Toast.makeText(this, "获取图片失败", Toast.LENGTH_SHORT).show(); } } } ================================================ FILE: app/src/main/java/com/example/xch/generateqrcode/QRCodeUtil.java ================================================ package com.example.xch.generateqrcode; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import android.support.annotation.Nullable; import android.text.TextUtils; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import java.util.Hashtable; public class QRCodeUtil { /** * 生成自定义二维码 * * @param content 字符串内容 * @param width 二维码宽度 * @param height 二维码高度 * @param character_set 编码方式(一般使用UTF-8) * @param error_correction_level 容错率 L:7% M:15% Q:25% H:35% * @param margin 空白边距(二维码与边框的空白区域) * @param color_black 黑色色块 * @param color_white 白色色块 * @param logoBitmap logo图片(传null时不添加logo) * @param logoPercent logo所占百分比 * @param bitmap_black 用来代替黑色色块的图片(传null时不代替) * @return */ public static Bitmap createQRCodeBitmap(String content, int width, int height, String character_set, String error_correction_level, String margin, int color_black, int color_white, Bitmap logoBitmap, float logoPercent, Bitmap bitmap_black) { // 字符串内容判空 if (TextUtils.isEmpty(content)) { return null; } // 宽和高>=0 if (width < 0 || height < 0) { return null; } try { /** 1.设置二维码相关配置,生成BitMatrix(位矩阵)对象 */ Hashtable hints = new Hashtable<>(); // 字符转码格式设置 if (!TextUtils.isEmpty(character_set)) { hints.put(EncodeHintType.CHARACTER_SET, character_set); } // 容错率设置 if (!TextUtils.isEmpty(error_correction_level)) { hints.put(EncodeHintType.ERROR_CORRECTION, error_correction_level); } // 空白边距设置 if (!TextUtils.isEmpty(margin)) { hints.put(EncodeHintType.MARGIN, margin); } /** 2.将配置参数传入到QRCodeWriter的encode方法生成BitMatrix(位矩阵)对象 */ BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); /** 3.创建像素数组,并根据BitMatrix(位矩阵)对象为数组元素赋颜色值 */ if (bitmap_black != null) { //从当前位图按一定的比例创建一个新的位图 bitmap_black = Bitmap.createScaledBitmap(bitmap_black, width, height, false); } int[] pixels = new int[width * height]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { //bitMatrix.get(x,y)方法返回true是黑色色块,false是白色色块 if (bitMatrix.get(x, y)) {// 黑色色块像素设置 if (bitmap_black != null) {//图片不为null,则将黑色色块换为新位图的像素。 pixels[y * width + x] = bitmap_black.getPixel(x, y); } else { pixels[y * width + x] = color_black; } } else { pixels[y * width + x] = color_white;// 白色色块像素设置 } } } /** 4.创建Bitmap对象,根据像素数组设置Bitmap每个像素点的颜色值,并返回Bitmap对象 */ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); /** 5.为二维码添加logo图标 */ if (logoBitmap != null) { return addLogo(bitmap, logoBitmap, logoPercent); } return bitmap; } catch (WriterException e) { e.printStackTrace(); return null; } } /** * 向二维码中间添加logo图片(图片合成) * * @param srcBitmap 原图片(生成的简单二维码图片) * @param logoBitmap logo图片 * @param logoPercent 百分比 (用于调整logo图片在原图片中的显示大小, 取值范围[0,1] ) * 原图片是二维码时,建议使用0.2F,百分比过大可能导致二维码扫描失败。 * @return */ @Nullable private static Bitmap addLogo(@Nullable Bitmap srcBitmap, @Nullable Bitmap logoBitmap, float logoPercent) { if (srcBitmap == null) { return null; } if (logoBitmap == null) { return srcBitmap; } //传值不合法时使用0.2F if (logoPercent < 0F || logoPercent > 1F) { logoPercent = 0.2F; } /** 1. 获取原图片和Logo图片各自的宽、高值 */ int srcWidth = srcBitmap.getWidth(); int srcHeight = srcBitmap.getHeight(); int logoWidth = logoBitmap.getWidth(); int logoHeight = logoBitmap.getHeight(); /** 2. 计算画布缩放的宽高比 */ float scaleWidth = srcWidth * logoPercent / logoWidth; float scaleHeight = srcHeight * logoPercent / logoHeight; /** 3. 使用Canvas绘制,合成图片 */ Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(srcBitmap, 0, 0, null); canvas.scale(scaleWidth, scaleHeight, srcWidth / 2, srcHeight / 2); canvas.drawBitmap(logoBitmap, srcWidth / 2 - logoWidth / 2, srcHeight / 2 - logoHeight / 2, null); return bitmap; } } ================================================ FILE: app/src/main/res/drawable/ic_launcher_background.xml ================================================ ================================================ FILE: app/src/main/res/drawable/list_bg.xml ================================================ ================================================ FILE: app/src/main/res/drawable-v24/ic_launcher_foreground.xml ================================================ ================================================ FILE: app/src/main/res/layout/activity_main.xml ================================================