1.å¨androidå¼åä¸ ä»ä¹åºå¯ä»¥è¯å«DMäºç»´ç
å¨androidå¼åä¸ ä»ä¹åºå¯ä»¥è¯å«DMäºç»´ç
1.1 åå¤å·¥ä½
å¦ææ们åªåäºç»´ç ççæï¼é£ä¹åªéè¦æ·»å æ ¸å¿jarå å³å¯ï¼
1.2 äºç»´ç çæ
OKï¼æ·»å å®jarå ä¹åæ们就å¯ä»¥å¼å§åäºç»´ç çæ代ç äºï¼äºç»´ç æ¬èº«å°±æ¯ä¸å¼ Bitmapå¾çï¼æ以æ们è¿é主è¦å°±æ¯çæä¹æ ·æ¥çæè¿å¼ å¾çï¼æå¨ä¸»çé¢æ·»å ä¸ä¸ªæé®åä¸ä¸ªImageViewï¼å½ç¹å»æé®æ¶çæä¸å¼ äºç»´ç å¾çæ¾ç¤ºå¨ImageViewä¸ãå¸å±å¦ä¸ï¼
[java] view plain copy print?维维码盘立方源码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="org.mobiletrain.qrwriter.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="generate"
android:text="çæäºç»´ç "/>
<ImageView
android:id="@+id/iv"
android:layout_width="dp"
android:layout_height="dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
å½æç¹å»æé®æ¶çæäºç»´ç å¾çï¼é£æ们就æ¥çççæäºç»´ç å¾ççæ ¸å¿ä»£ç ï¼
[java] view plain copy print?
private Bitmap generateBitmap(String content,int width, int height) {
QRCodeWriter qrCodeWriter = new QRCodeWriter();
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
try {
BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
int[] pixels = new int[width * height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (encode.get(j, i)) {
pixels[i * width + j] = 0x;
} else {
pixels[i * width + j] = 0xffffffff;
}
}
}
return Bitmap.createBitmap(pixels, 0, width, width, height, Bitmap.Config.RGB_);
} catch (WriterException e) {
e.printStackTrace();
}
return null;
}
é¦å è¿ä¸ªæ¹æ³æ¥æ¶ä¸ä¸ªåæ°ï¼è¿ä¸ä¸ªåæ°åå«è¡¨ç¤ºçæäºç»´ç çææ¬å 容ï¼ä½ è¦æåªä¸ä¸ªææ¬ç¨äºç»´ç å¾ç表示åºæ¥ï¼ï¼ç¬¬äºä¸ªå第ä¸ä¸ªåæ°åå«è¡¨ç¤ºçæçäºç»´ç å¾çç宽åé«ãå¨è¿éï¼æ们é¦å è¦è·å¾ä¸ä¸ªQRCodeWriterå®ä¾ï¼è¯¥å®ä¾ä¸æä¸ä¸ªæ¹æ³å«åencodeï¼éè¿è¯¥æ¹æ³å¯¹ææ¬å 容è¿è¡ç¼ç ï¼è¯¥æ¹æ³å ±æäºä¸ªåæ°ï¼ç¬¬ä¸ä¸ªåæ°è¡¨ç¤ºçæäºç»´ç çææ¬å 容ï¼ç¬¬äºä¸ªåæ°è¡¨ç¤ºç¼ç æ ¼å¼ï¼ç¬¬ä¸ä¸ªåæ°è¡¨ç¤ºçæçäºç»´ç ç宽度ï¼ç¬¬å个åæ°è¡¨ç¤ºçæçäºç»´ç çé«åº¦ï¼ç¬¬äºä¸ªåæ°å¯éï¼å¯ä»¥ç¨æ¥è®¾ç½®ææ¬çç¼ç ï¼encodeæ¹æ³çè¿åå¼æ¯ä¸ä¸ªBitMatrixï¼ä½ å¯ä»¥æBitMatrixç解æä¸ä¸ªäºç»´æ°ç»ï¼è¿ä¸ªäºç»´æ°ç»çæ¯ä¸ä¸ªå ç´ é½è¡¨ç¤ºä¸ä¸ªåç´ ç¹æ¯å¦ææ°æ®ãOKï¼æ¥ä¸æ¥æ们éè¦å®ä¹ä¸ä¸ªintæ°ç»ç¨æ¥åæ¾Bitmapä¸ææåç´ ç¹çé¢è²ï¼å¯æ¯æ们åæä¹ç¥éæ¯ä¸ä¸ªåç´ ç¹æ¯ä»ä¹é¢è²å¢ï¼è¿ä¸ªæ¶åå°±éè¦æ们éåBitMatrixäºï¼å¦æBitMatrixä¸çç¹è¡¨ç¤º 该ç¹ææ°æ®ï¼é£ä¹å¯¹åºå¨Bitmapä¸çåç´ ç¹å°±æ¯é»è²ï¼å¦åå°±æ¯ç½è²ãBitMatrixä¸çgetæ¹æ³çè¿åå¼ä¸ºä¸ä¸ªbooleanç±»åï¼true表示该ç¹ææ°æ®ï¼false表示该ç¹æ²¡ææ°æ®ãéè¿ä¸¤ä¸ªåµå¥çfor循ç¯å°BitMatrixéåä¸éï¼ç¶åç»pixelsæ°ç»é½èµä¸å¼ï¼OKï¼pixelsæ°ç»æå¼ä¹åï¼æ¥ä¸æ¥è°ç¨BitmapçcreateBitmapæ¹æ³å建ä¸ä¸ªBitmapåºæ¥å°±å¯ä»¥äºï¼createBitmapæ¹æ³å ±æ¥æ¶6个åæ°ï¼ç¬¬ä¸ä¸ªåæ°è¡¨ç¤ºBitmapä¸ææåç´ ç¹çé¢è²ï¼ç¬¬äºä¸ªåæ°è¡¨ç¤ºåç´ ç¹çå移éï¼ç¬¬ä¸ä¸ªåæ°è¡¨ç¤ºBitmapæ¯è¡æå¤å°ä¸ªåç´ ç¹ï¼ç¬¬å个åæ°è¡¨ç¤ºçæçBitmapç宽度ï¼ç¬¬äºä¸ªåæ°è¡¨ç¤ºçæçBitmapçé«åº¦ï¼ç¬¬å 个åæ°è¡¨ç¤ºçæçBitmapçè²å½©æ¨¡å¼ï¼å 为äºç»´ç åªæé»ç½ä¸¤ç§é¢è²ï¼æ以æ们å¯ä»¥ä¸ç¨èèéæ度ï¼ç´æ¥ä½¿ç¨RGB_å³å¯ãOKï¼è¿æ ·çè¯æ们就è·åå°äºäºç»´ç çå¾çäºï¼æåæ们åæ¥ççç¹å»äºä»¶ï¼
[java] view plain copy print?
public void generate(View view) {
Bitmap qrBitmap = generateBitmap("",, );
iv.setImageBitmap(qrBitmap);
}