ZXing二维图

public class TestQrcode {
	
	/**
	 * @introduction 设置颜色值,由于后来的setRGB方法
	 * @author Guo
	 */
	private static final int BLACK = 0xff000000;
	private static final int WHITE = 0xFFFFFFFF;


	public static void main(String[] args) {
		
		TestQrcode test = new TestQrcode();
		File file = new File("G://Picture//qrcode.png");
		String str = "很好";
		
		try {
			test.encode(new String(str.getBytes("UTF-8"), "ISO-8859-1"), file, BarcodeFormat.QR_CODE, 200, 200);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		/**
		 * @introduction 解析二维码图片的信息
		 * @author Guo
		 */
		//test.decode(file);

	}
	
	public void encode(String contents, File file, BarcodeFormat format, int width, int height) {
		
		try {
			/**
			 * @introduction BitMatrix是构建二维矩阵位图的类
			 * @introduction 通过MultiFormatWriter类编码二维码的格式
			 * @author Guo
			 */
			BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height);
			writeToFile(bitMatrix, "png", file);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
		
		/**
		 * @introduction BufferedImage描述具有可访问图像数据缓冲区的 Image
		 * @author Guo
		 */
		BufferedImage image = toBufferedImage(matrix);
		
		/**
		 * @introduction ImageIO该类包含一些用来查找 ImageReader 和 ImageWriter 以及执行简单编码和解码的静态便捷方法
		 * @introduction write方法使用支持给定格式的任意 ImageWriter 将一个图像写入 File
		 * @author Guo
		 */
		ImageIO.write(image, format, file);
	}
	
	public BufferedImage toBufferedImage(BitMatrix bitMatrix) {
		
		/**
		 * @introduction 拿到位图的基本数据
		 * @author Guo
		 */
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		
		/**
		 * @introduction 构造一个类型为预定义图像类型之一的 BufferedImage
		 * @author Guo
		 */
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		
		/**
		 * @introduction 设置黑白位点
		 * @author Guo
		 */
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) == true ? BLACK : WHITE);
			}
		}
		return image;
	}
	
	public void decode(File file) {
		
		BufferedImage image;
		
		try {
			image = ImageIO.read(file);
			
			if(image == null)
				System.out.println("Can not find the image!");
			else {
				
				LuminanceSource source = new BufferedImageLuminanceSource(image);
				BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
				
				Result result;
				Hashtable hints = new Hashtable();
				hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
				result = new MultiFormatReader().decode(bitmap, hints);
				String resultStr = result.getText();
				System.out.println("解析后内容:" + resultStr);

			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (NotFoundException e) {
			e.printStackTrace();
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Zxing(Zebra Crossing)是一个开源的条码扫描和生成库。它提供了丰富的功能,包括生成二维码数据。 要生成二维码,我们首先需要添加Zxing库的依赖。通常,我们会在项目的build.gradle文件中的dependencies块中添加以下行: ``` implementation 'com.google.zxing:core:3.4.1' implementation 'com.journeyapps:zxing-android-embedded:3.6.0' ``` 接下来,我们可以使用Zxing库提供的函数来生成二维码。以下是一个简单的代码示例: ```java import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import androidx.appcompat.app.AppCompatActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; public class MainActivity extends AppCompatActivity { private EditText inputText; private Button generateButton; private ImageView qrCodeImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inputText = findViewById(R.id.input_text); generateButton = findViewById(R.id.generate_button); qrCodeImage = findViewById(R.id.qrcode_image); generateButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String text = inputText.getText().toString(); try { // 设置二维码的宽高 int width = 500; int height = 500; // 生成二维码的格式 BarcodeFormat format = BarcodeFormat.QR_CODE; BitMatrix bitMatrix = new MultiFormatWriter().encode(text, format, width, height); // 将BitMatrix转为Bitmap Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { bitmap.setPixel(x, y, bitMatrix.get(x, y) ? Color.BLACK : Color.WHITE); } } qrCodeImage.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } }); } } ``` 上述代码会在点击按钮时生成输入文本的二维码,并显示在ImageView上。我们可以根据需要设置生成的二维码的宽高和格式,以及其他自定义属性。 通过上述代码,我们可以轻松地使用Android Zxing库生成二维码数据。希望这能够帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值