android 生成二维码(带图片&不带图片),2024年最新安卓大厂面试笔试题分享

return null;

}

return bitmap;

}

2、生成带图片的二维码

===========

/**

  • @Title: createQRImage带图片

  • @param url

  •        可以是链接地址,也可以是文字
    
  • @param QR_WIDTH

  •        二维码宽度
    
  • @param QR_HEIGHT

  •        二维码高度
    
  • @param mBitmap

  •        图片
    
  • @return Bitmap 返回一个bitmap,可以自行保存到本地,也可以设置显示在页面

*/

public static Bitmap createQRImage(String url, int QR_WIDTH, int QR_HEIGHT,

Bitmap mBitmap,Context context) {

try {

Matrix m = new Matrix();

float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();

float sy = (float) 2 * IMAGE_HALFWIDTH

/ mBitmap.getHeight();

m.setScale(sx, sy);

mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,

mBitmap.getWidth(), mBitmap.getHeight(), m, false);

String contentString = url;

mBitmap = cretaeBitmap(mBitmap,new String(contentString.getBytes(),“utf-8”),context);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (WriterException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return mBitmap;

}

private static final int IMAGE_HALFWIDTH = 40;//二维码的宽度

public static Bitmap cretaeBitmap(Bitmap mBitmap,String str,Context context) throws WriterException {

Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

hints.put(EncodeHintType.CHARACTER_SET, “utf-8”);

hints.put(EncodeHintType.MARGIN, 1);

BitMatrix matrix = new MultiFormatWriter().encode(str,

BarcodeFormat.QR_CODE, Utils.dip2px(context, 300), Utils.dip2px(context, 300), hints);

int width = matrix.getWidth();

int height = matrix.getHeight();

int halfW = width / 2;

int halfH = height / 2;

int[] pixels = new int[width * height];

for (int y = 0; y < height; y++) {

for (int x = 0; x < width; x++) {

if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

本文在开源项目GitHub中已收录,里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…

目前已经更新的部分资料,需要的自己取:



目前已经更新的部分资料,需要的自己取:

[外链图片转存中…(img-wwI8cvIB-1711136871690)]
[外链图片转存中…(img-TX1TIZ8i-1711136871690)]
[外链图片转存中…(img-QdL50OZD-1711136871690)]

  • 25
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android生成二维码图片并打印,你可以使用以下步骤: 1. 添加依赖项:在 build.gradle 文件中添加以下依赖项: ``` implementation 'com.google.zxing:core:3.4.0' ``` 2. 生成二维码:使用以下代码生成二维码图片 ```java String content = "your_content_here"; int width = 500; int height = 500; QRCodeWriter writer = new QRCodeWriter(); try { BitMatrix bitMatrix = writer.encode(content, BarcodeFormat.QR_CODE, width, height); int bitmapWidth = bitMatrix.getWidth(); int bitmapHeight = bitMatrix.getHeight(); Bitmap bmp = Bitmap.createBitmap(bitmapWidth, bitmapHeight, Bitmap.Config.RGB_565); for (int x = 0; x < bitmapWidth; x++) { for (int y = 0; y < bitmapHeight; y++) { bmp.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } // bmp 即为生成的二维码图片 } catch (WriterException e) { e.printStackTrace(); } ``` 3. 打印二维码:将生成的二维码图片通过打印机打印出来。你可以使用 Android 的打印机 API 来实现打印功能,或者使用第三方打印库。 示例代码: ```java // 获取打印服务 PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); // 创建打印任务 PrintDocumentAdapter adapter = new PrintDocumentAdapter() { @Override public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { try { OutputStream out = new FileOutputStream(destination.getFileDescriptor()); bmp.compress(Bitmap.CompressFormat.PNG, 100, out); callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES}); out.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { callback.onLayoutFinished(new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build(), true); } }; // 执行打印任务 String jobName = "二维码打印任务"; printManager.print(jobName, adapter, null); ``` 注意:以上代码仅供参考,实际使用中需要根据具体需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值