直播视频app源码,Android 点击生成二维码实现的相关代码
activity.xml代码如下:
<?xml version&#61;"1.0" encoding&#61;"utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:layout_width&#61;"match_parent"android:layout_height&#61;"match_parent"android:orientation&#61;"vertical"><EditTextandroid:id&#61;"&#64;&#43;id/create_qr_content"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content" /><LinearLayoutandroid:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content"android:orientation&#61;"horizontal"><Buttonandroid:id&#61;"&#64;&#43;id/create_qr_btn"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"生成二维码" /><CheckBoxandroid:id&#61;"&#64;&#43;id/create_qr_addLogo"android:layout_width&#61;"wrap_content"android:layout_height&#61;"wrap_content"android:text&#61;"添加logo图案" /></LinearLayout><ImageViewandroid:id&#61;"&#64;&#43;id/create_qr_iv"android:layout_width&#61;"match_parent"android:layout_height&#61;"wrap_content" />
</LinearLayout>
QRCodeUtil工具类
public class QRCodeUtil {public static boolean createQRImage(String content, int widthPix, int heightPix, Bitmap logoBm, String filePath) {try {if (content &#61;&#61; null || "".equals(content)) {return false;}//配置参数Map<EncodeHintType, Object> hints &#61; new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "utf-8");//容错级别hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置空白边距的宽度
// hints.put(EncodeHintType.MARGIN, 2); //default is 4// 图像数据转换&#xff0c;使用了矩阵转换BitMatrix bitMatrix &#61; new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, widthPix, heightPix, hints);int[] pixels &#61; new int[widthPix * heightPix];// 下面这里按照二维码的算法&#xff0c;逐个生成二维码的图片&#xff0c;// 两个for循环是图片横列扫描的结果for (int y &#61; 0; y < heightPix; y&#43;&#43;) {for (int x &#61; 0; x < widthPix; x&#43;&#43;) {if (bitMatrix.get(x, y)) {pixels[y * widthPix &#43; x] &#61; 0xff000000;} else {pixels[y * widthPix &#43; x] &#61; 0xffffffff;}}}// 生成二维码图片的格式&#xff0c;使用ARGB_8888Bitmap bitmap &#61; Bitmap.createBitmap(widthPix, heightPix, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, widthPix, 0, 0, widthPix, heightPix);if (logoBm !&#61; null) {bitmap &#61; addLogo(bitmap, logoBm);}//必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的&#xff0c;内存消耗巨大&#xff01;return bitmap !&#61; null && bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(filePath));} catch (WriterException | IOException e) {e.printStackTrace();}return false;}/*** 在二维码中间添加Logo图案*/private static Bitmap addLogo(Bitmap src, Bitmap logo) {if (src &#61;&#61; null) {return null;}if (logo &#61;&#61; null) {return src;}//获取图片的宽高int srcWidth &#61; src.getWidth();int srcHeight &#61; src.getHeight();int logoWidth &#61; logo.getWidth();int logoHeight &#61; logo.getHeight();if (srcWidth &#61;&#61; 0 || srcHeight &#61;&#61; 0) {return null;}if (logoWidth &#61;&#61; 0 || logoHeight &#61;&#61; 0) {return src;}//logo大小为二维码整体大小的1/5float scaleFactor &#61; srcWidth * 1.0f / 5 / logoWidth;Bitmap bitmap &#61; Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);try {Canvas canvas &#61; new Canvas(bitmap);canvas.drawBitmap(src, 0, 0, null);canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);canvas.save();canvas.restore();} catch (Exception e) {bitmap &#61; null;e.getStackTrace();}return bitmap;}
}
Activity 代码如下
&#64;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_shop);//内容final EditText contentET &#61; (EditText) findViewById(R.id.create_qr_content);//显示二维码图片final ImageView imageView &#61; (ImageView) findViewById(R.id.create_qr_iv);//是否添加Logofinal CheckBox addLogoCB &#61; (CheckBox) findViewById(R.id.create_qr_addLogo);Button createQrBtn &#61; (Button) findViewById(R.id.create_qr_btn);createQrBtn.setOnClickListener(new View.OnClickListener() {&#64;Overridepublic void onClick(View v) {final String filePath &#61; getFileRoot(ShopActivity.this) &#43; File.separator&#43; "qr_" &#43; System.currentTimeMillis() &#43; ".jpg";//二维码图片较大时&#xff0c;生成图片、保存文件的时间可能较长&#xff0c;因此放在新线程中new Thread(new Runnable() {&#64;Overridepublic void run() {boolean success &#61; QRCodeUtil.createQRImage(contentET.getText().toString().trim(), 800, 800,addLogoCB.isChecked() ? BitmapFactory.decodeResource(getResources(), R.mipmap.heartsong) : null,filePath);if (success) {runOnUiThread(new Runnable() {&#64;Overridepublic void run() {imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));}});}}}).start();}});}//文件存储根目录private String getFileRoot(Context context) {if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {File external &#61; context.getExternalFilesDir(null);if (external !&#61; null) {return external.getAbsolutePath();}}return context.getFilesDir().getAbsolutePath();}
以上就是直播视频app源码&#xff0c;Android 点击生成二维码实现的相关代码&#xff0c; 更多内容欢迎关注之后的文章