生成联系人二维码

    xiaoxiao2021-03-25  238

    二维码本质就是字符串转换成图像。二维码的生成使用zxing开源库,可以不用管二维码具体的原理和怎么转换也能完成功能,所以生成联系人最重要的是怎么获得要转换的字符串,其实这个早就有标准了,就是vcard。见百度百科:

    vCard 规范容许公开交换个人数据交换 (Personal Data Interchange PDI) 信息,在传统纸质商业名片可找到这些信息。规范定义电子名片(或叫vCard)的格式。 vCard 规范可作为各种应用或系统之间的交换格式。定义的格式与传送的方法无关。传送交换可能是文件系统,点对点交换的公共电话网络,以有线网络或无线传送的方式。用户能在互联网上直接利用vCard。电子邮件能转发在vCard中人信息。网页上很多用户填写的表格可自动使用vCard。

    Android源码中就有vcard相关实现,见frameworks/opt/vcard目录下,会单独编译成jar包。

    为了使用这两个库,在android.mk中药加入这两个jar包:

    LOCAL_STATIC_JAVA_LIBRARIES += com.android.vcard LOCAL_STATIC_JAVA_LIBRARIES += zxing

    需要import的类:

    import android.provider.ContactsContract.RawContactsEntity; import com.android.vcard.VCardComposer; import com.android.vcard.VCardConfig; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter;

    生成联系人vcard字符串:

    private String getQrcodeString() { VCardComposer composer = new VCardComposer(this, VCardConfig.VCARD_TYPE_V30_GENERIC | VCardConfig.FLAG_REFRAIN_IMAGE_EXPORT, true); final Uri contentUriForRawContactsEntity = RawContactsEntity.CONTENT_URI .buildUpon() .appendQueryParameter(RawContactsEntity.FOR_EXPORT_ONLY, "1") .build(); if (!composer.init(simOrPhoneUri, null, null, null, null, contentUriForRawContactsEntity)) { ... return ""; } if (!composer.isAfterLast()) { return composer.createOneEntry(); } return ""; }其中simOrPhoneUri是指定联系人的uri,所以后续四个都传递了null,如果没有指定联系人的uri,那么可以传递查询的参数selection和selectionArgs进去,和一般的数据库查询query没啥区别,见方法定义frameworks/opt/vcard/java/com/android/vcard/VCardComposer.java

    public boolean init(final Uri contentUri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder, Uri contentUriForRawContactsEntity)由字符串生成二维码图像并显示:

    private void showQrcodeImage() { String qrcode = getQrcodeString(); Log.d(TAG, "showQrcodeImage = " + qrcode); Map<EncodeHintType, String> hints = new HashMap<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 图像数据转换,使用了矩阵转换 try { BitMatrix bitMatrix = new QRCodeWriter().encode(qrcode, BarcodeFormat.QR_CODE, 400, 400, hints); int[] pixels = new int[400 * 400]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < 400; y++) { for (int x = 0; x < 400; x++) { if (bitMatrix.get(x, y)) { pixels[y * 400 + x] = 0xff000000; } else { pixels[y * 400 + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, 400, 0, 0, 400, 400); if (myDialog != null) { myDialog.dismiss(); myDialog = null; } myDialog = new AlertDialog.Builder(this).create(); myDialog.show(); myDialog.getWindow().setContentView(R.layout.qrcode_dialog); myDialog.getWindow().setGravity(Gravity.CENTER); ImageView v = (ImageView) myDialog.getWindow().findViewById( R.id.qrcode_image); v.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); } }myDialog是AlertDialog,qrcode_dialog是包含一个imageView的布局文件。这段代码是我从网上copy并修改的,可是比较早实在是忘记出处了。 由于二维码传递的就是字符串,所以扫描二维码得到字符串后后续怎么运行是扫描端app决定的。例如微信扫描vcard二维码后可以跳转到新建联系人的Activity,而淘宝扫描后没有反应,只是把字符串打印显示在屏幕上。

    转载请注明原文地址: https://ju.6miu.com/read-5265.html

    最新回复(0)