扫描二维码
package com.baidu.erweima; import com.google.zxing.WriterException; import com.zxing.activity.CaptureActivity; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.util.DisplayMetrics; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; public class MainActivity extends Activity { private Button atart; private EditText edit; private Button both; private ImageView image; int mScreenWidth; private Button web; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { atart = (Button) findViewById(R.id.start); edit = (EditText) findViewById(R.id.edit); both = (Button) findViewById(R.id.both); image = (ImageView) findViewById(R.id.iv_qr_image); web = (Button) findViewById(R.id.start_web); //得到屏幕的宽度 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); mScreenWidth = dm.widthPixels; //开始扫描二维码 atart.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 调用ZXIng开源项目源码 扫描二维码 Intent openCameraIntent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(openCameraIntent, 0); } }); //生成二维码 both.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String uri = edit.getText().toString(); Bitmap bitmap; //加入头像,并生成彩色的二维码 Bitmap decodeResource = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.account_icon_qzone); try { bitmap = BitmapUtil.makeQRImage(decodeResource,uri, 400,400); if(bitmap != null){ image.setImageBitmap(bitmap); } } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } //加入头像 //bitmap = addLogo(bitmap, BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.ic_launcher)); //添加水印 // Bitmap gainBitmap = BitmapUtil.gainBitmap(getApplicationContext(),R.drawable.y); // bitmap=BitmapUtil.composeWatermark(bitmap ,gainBitmap ); //添加图片背景 // Bitmap gainBitmap2 = BitmapUtil.gainBitmap(getApplication(),R.drawable.u); // bitmap=BitmapUtil.addBackground(bitmap, gainBitmap2); } }); web.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String text=edit.getText().toString().trim(); /*Intent intent=new Intent(MainActivity.this,ShowActivity.class); intent.putExtra("text", text); startActivity(intent);*/ Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse(text); intent.setData(content_url); startActivity(intent); } }); } /** * 在二维码中间添加Logo图案 */ private static Bitmap addLogo(Bitmap src, Bitmap logo) { if (src == null) { return null; } if (logo == null) { return src; } //获取图片的宽高 int srcWidth = src.getWidth(); int srcHeight = src.getHeight(); int logoWidth = logo.getWidth(); int logoHeight = logo.getHeight(); if (srcWidth == 0 || srcHeight == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return src; } //logo大小为二维码整体大小的1/5 float scaleFactor = srcWidth * 1.0f / 5 / logoWidth; Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888); try { Canvas canvas = 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.ALL_SAVE_FLAG); canvas.restore(); } catch (Exception e) { bitmap = null; e.getStackTrace(); } return bitmap; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); // 取得返回信息 if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); String scanResult = bundle.getString("result"); edit.setText(scanResult); } } }
//BitmapUtils
package com.baidu.erweima; import java.util.Hashtable; import java.util.Random; import android.app.ActionBar.LayoutParams; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PointF; import android.util.Log; import android.view.Gravity; import android.view.View.MeasureSpec; import android.widget.LinearLayout; import android.widget.TextView; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; public class BitmapUtil { // 将图片变为Bitmap形式 public static Bitmap gainBitmap(Context context, int drawableId) { Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawableId); return bmp; } /** * 生成彩色的二维码 * */ public static Bitmap makeQRImage(Bitmap logoBmp, String content, int QR_WIDTH, int QR_HEIGHT) throws WriterException { try { // 图像数据转换,使用了矩阵转换 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容错率 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { // 下面这里按照二维码的算法,逐个生成二维码的图片,//两个for循环是图片横列扫描的结果 for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { if (x < QR_WIDTH / 2 && y < QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF0094FF;// 蓝色 Integer.toHexString(new Random().nextInt()); } else if (x < QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFFFF0000;// 红色 } else if (x > QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF5ACF00;// 绿色 } else { pixels[y * QR_WIDTH + x] = 0xFF000000;// 黑色 } } else { pixels[y * QR_WIDTH + x] = 0xffffffff;// 白色 } } } // ------------------添加图片部分------------------// Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); // 设置像素点 bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); // 获取图片宽高 int logoWidth = logoBmp.getWidth(); int logoHeight = logoBmp.getHeight(); if (QR_WIDTH == 0 || QR_HEIGHT == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return bitmap; } // 图片绘制在二维码中央,合成二维码图片 // logo大小为二维码整体大小的1/4 float scaleFactor = QR_WIDTH * 1.0f / 4 / logoWidth; try { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, 0, null); canvas.scale(scaleFactor, scaleFactor, QR_WIDTH / 2, QR_HEIGHT / 2); canvas.drawBitmap(logoBmp, (QR_WIDTH - logoWidth) / 2, (QR_HEIGHT - logoHeight) / 2, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return bitmap; } catch (Exception e) { bitmap = null; e.getStackTrace(); } } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 在图片右下角添加水印 * * @param srcBMP * 原图 * @param markBMP * 水印图片 * @return 合成水印后的图片 */ public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) { if (srcBMP == null) { return null; } // 创建一个新的和SRC长度宽度一样的位图 Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(), srcBMP.getHeight(), Bitmap.Config.ARGB_8888); Canvas cv = new Canvas(newb); // 在 0,0坐标开始画入原图 cv.drawBitmap(srcBMP, 0, 0, null); // 在原图的右下角画入水印 cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth() * 4 / 5, srcBMP.getHeight() * 2 / 7, null); // 保存 cv.save(Canvas.ALL_SAVE_FLAG); // 存储 cv.restore(); return newb; } // 添加背景图片 public static Bitmap addBackground(Bitmap foreground, Bitmap background) { //得到图片的宽和高 int bgWidth = background.getWidth(); int bgHeight = background.getHeight(); //得到二维码的宽和高 int fgWidth = foreground.getWidth(); int fgHeight = foreground.getHeight(); Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newmap); canvas.drawBitmap(background, 0, 0, null); canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2, (bgHeight - fgHeight) * 3 / 5 + 70, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return newmap; } }