一、生成二维码
1、布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_horizontal" > <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成二维码"/> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="生成自定义二维码"/> <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>2、Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt1= (Button) findViewById(R.id.bt1); Button bt2= (Button) findViewById(R.id.bt2); iv = (ImageView) findViewById(R.id.iv); bt1.setOnClickListener(this); bt2.setOnClickListener(this); } @Override public void onClick(View v) { Bitmap bitmap=null; switch (v.getId()){ case R.id.bt1: bitmap=getBitmap("http://www.360kan.com/dianying/index.html",400,400); iv.setImageBitmap(bitmap); break; case R.id.bt2: bitmap=getBitmap("http://www.360kan.com/dianying/index.html",400,400); Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher); Bitmap bitmap1=getLogoBitmap(bitmap,logoBitmap); iv.setImageBitmap(bitmap1); break; } } private Bitmap getLogoBitmap(Bitmap bitmap, Bitmap logoBitmap) { int width = bitmap.getWidth(); int height = bitmap.getHeight(); int logoBitmapWidth = logoBitmap.getWidth(); int logoBitmapHeight = logoBitmap.getHeight(); Bitmap cusbitmap=Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); Canvas canvas=new Canvas(cusbitmap); canvas.drawBitmap(bitmap,0,0,null); canvas.save(Canvas.ALL_SAVE_FLAG); float scaleSize = 1.0f; while ((logoBitmapWidth / scaleSize) > (width / 5) || (logoBitmapHeight / scaleSize) > (height / 5)) { scaleSize *= 2; } float sx = 1.0f / scaleSize; canvas.scale(sx, sx, width / 2, height / 2); canvas.drawBitmap(logoBitmap, (width - logoBitmapWidth) / 2, (height - logoBitmapHeight) / 2, null); canvas.restore(); return cusbitmap; } private Bitmap getBitmap(String content, int width, int height) { QRCodeWriter qrCodeWriter=new QRCodeWriter(); Map<EncodeHintType,String> hint=new HashMap<>(); hint.put(EncodeHintType.CHARACTER_SET,"utf-8"); try { BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height); 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] = 0x00000000; } else { pixels[i * width + j] = 0xffffffff; } } } return Bitmap.createBitmap(pixels,0,width,width,height, Bitmap.Config.RGB_565); } catch (Exception e) { e.printStackTrace(); } return null; } }二、扫描二维码
文档
http://note.youdao.com/noteshare?id=9f3c12f4303e5183c34bfcf27d8c0319