最近研究了一下java zxing生成二维码码可是不懂他这是什么算法ZXing的

最近研究了一下二维码可是不懂他这是什么算法ZXing的_百度知道
最近研究了一下二维码可是不懂他这是什么算法ZXing的
我有更好的答案
二维码分两类:行排式二维条码和矩阵式二维码,已矩阵式二维码中QRcode为例。 QRcode是日本Denso公司开发的,目前网上有其二维码信息组件QRcode.dll,该组件可对输入的字符串进行QR编码 调用接口定义: STDMETHOD IMPCQREncoder:Encode
其他类似问题
为您推荐:
二维码的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁Xamarin.Android-用ZXing实现二维码扫描以及连续扫描 - 推酷
Xamarin.Android-用ZXing实现二维码扫描以及连续扫描
本文的内容有两个基础:ZXing.Net和ZXing.Net.Mobile
ZXing.Net:ZXing的C#实现,主要封装了各种二维码的编码、解码等跨平台的算法
ZXing.Net.Mobile:对ZXing.Net在xamarin的应用进行了封装,主要实现了摄像头扫描、扫描view、扫描activity、扫描Fragment等
ZXing.Net.Mobile下载:
二、效果图
三、基本实现
1、弹出新窗口进行扫描
public class MainFragment : Android.Support.V4.App.Fragment
MobileBarcodeS
public override View OnCreateView(LayoutInflater inflater, ViewGroup p1, Bundle p2)
return inflater.Inflate(Resource.Layout.Main, null);
public override void OnViewCreated(View view, Bundle savedInstanceState)
base.OnViewCreated(view, savedInstanceState);
scanner = new MobileBarcodeScanner(this.Activity);
this.View.FindViewById&Button&(Resource.Id.btnDefault).Click += btnDefault_C
this.View.FindViewById&Button&(Resource.Id.btnCustom).Click += btnCustom_C
async void btnDefault_Click(object sender, EventArgs e)
//不使用自定义界面
scanner.UseCustomOverlay = false;
//设置上下提示文字
scanner.TopText = &上面的文字&;
scanner.BottomText = &下面的文字&;
var result = await scanner.Scan();
HandleScanResult(result);
async void btnCustom_Click(object sender, EventArgs e)
View zxingO
//使用自定义界面(可以给框内加个动画什么的,这个自由发挥)
scanner.UseCustomOverlay = true;
zxingOverlay = LayoutInflater.FromContext(this.Activity).Inflate(Resource.Layout.ZxingOverlay, null);
scanner.CustomOverlay = zxingO
var result = await scanner.Scan();
HandleScanResult(result);
void HandleScanResult(ZXing.Result result)
string msg = &&;
if (result != null && !string.IsNullOrEmpty(result.Text))
msg = &扫描结果: & + result.T
msg = &扫描取消!&;
this.Activity.RunOnUiThread(() =&
Toast.MakeText(this.Activity, msg, ToastLength.Short).Show();
btnDefault_Click中使用了默认的弹出扫描,可定制性差,只能修改上下的文字
btnCustom_Click中使用了自定义界面弹出扫描,其实就是给扫描界面上面覆盖一层自定义的布局
在HandleScanResult中进行扫描结果的处理,这里只是简单的Toast
2、Fragment中集成扫描
这里使用了ZXing.Net.Mobile库中已经封装好的ZXingScannerFragment,并使用了覆盖层进行界面自定义
public class ScannerFragment : Android.Support.V4.App.Fragment
ZXingScannerFragment scanF
View zxingO
public override View OnCreateView(LayoutInflater inflater, ViewGroup p1, Bundle p2)
return inflater.Inflate(Resource.Layout.Scanner, null);
public override void OnViewCreated(View view, Bundle savedInstanceState)
base.OnViewCreated(view, savedInstanceState);
zxingOverlay = LayoutInflater.FromContext(this.Activity).Inflate(Resource.Layout.ZxingOverlay, null);
scanFragment = new ZXingScannerFragment(ScanResultCallback);
scanFragment.UseCustomView = true;
scanFragment.CustomOverlayView = zxingO
this.Activity.SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.fragment_container, scanFragment)
.Commit();
private void ScanResultCallback(ZXing.Result result)
if (result == null || string.IsNullOrEmpty(result.Text))
this.Activity.RunOnUiThread(() =&
Toast.MakeText(this.Activity, &扫描已取消!&, ToastLength.Short).Show();
((HomeFragment)this.FragmentManager.Fragments[0]).SetCurrentTab(&Main&);
//扫描成功
this.Activity.RunOnUiThread(() =&
Vibrator vibrator = (Vibrator)Application.Context.GetSystemService(Context.VibratorService);
long[] pattern = { 0, 350, 220, 350 };
vibrator.Vibrate(pattern, -1);
Console.WriteLine(result.Text);
Toast.MakeText(this.Activity, result.Text, ToastLength.Short).Show();
((HomeFragment)this.FragmentManager.Fragments[0]).SetCurrentTab(&Main&);
初始化ZXingScannerFragment,为其传入一个Action&ZXing.Result&类型的ScanResultCallback回调,
在ScanResultCallback中进行扫描结果处理:震动、输出、Toast、切换Fragment等
四、连续扫描
现在基本能扫描了,不过现在有个需求:扫描后如果扫描结果不是手机号码则继续扫描。
OK,修改一下扫描完成后的回调处理
//此处加上二维码的格式要求,如果不符合要求,就继续扫描(我这里是判断是否是手机号码)
if (IsTelephone(result.Text))
Toast.MakeText(this.Activity, result.Text, ToastLength.Short).Show();
((HomeFragment)this.FragmentManager.Fragments[0]).SetCurrentTab(&Main&);
Toast.MakeText(this.Activity, &扫描的二维码格式不正确!&, ToastLength.Short).Show();
/// &summary&
/// 验证手机号码的格式
/// &/summary&
public bool IsTelephone(string str_telephone)
return Regex.IsMatch(str_telephone, @&^(0|86|17951)?(13[0-9]|15[]|17[678]|18[0-9]|14[57])[0-9]{8}$&);
本来以为这样就搞定了,没想到扫描结果不是手机号时,提示&&扫描的二维码格式不正确!&后便停止扫描了。
看来ZXing.Net.Mobile库只要扫描完成后,就会停止扫描(我想作者是为了防止重复扫描的问题吧)。
为了实现连续扫描就只能修改ZXing.Net.Mobile库了
下面只是“Fragment中集成扫描”中的实现,“弹出新窗口进行扫描”也有类似的问题,小伙伴们自己查看源码发挥吧,哈哈!
经过分析发现ZXingSurfaceView类中的OnPreviewFrame方法在扫描完成后,触发回调前,调用了ShutdownCamera ();
这便是停止扫描的罪魁祸首,OK,将其抹杀,这时是可以连续扫描了,不过即使扫描结果是手机号扫描还会继续,这样就有可能出现重复扫描的问题,
因此这种情况下,扫描结果符合要求后,需要手动“停止扫描”:ZXingScannerFragment.Shutdown()
//此处加上二维码的格式要求,如果不符合要求,就继续扫描(我这里是判断是否是手机号码)
if (IsTelephone(result.Text))
//主动关闭摄像头,防止重复扫描
scanFragment.Shutdown();
Toast.MakeText(this.Activity, result.Text, ToastLength.Short).Show();
((HomeFragment)this.FragmentManager.Fragments[0]).SetCurrentTab(&Main&);
Toast.MakeText(this.Activity, &扫描的二维码格式不正确!&, ToastLength.Short).Show();
如果你觉得文章对你有帮助,可以点击旁边的“推荐”按钮,这样会让更多需要的人有机会看到
已发表评论数()
&&登&&&录&&
已收藏到推刊!
请填写推刊名
描述不能大于100个字符!
权限设置: 公开
仅自己可见页面导航:
→ 正文内容 C# zxing二维码写入
C# zxing二维码写入的实例代码
C# zxing二维码写入的实例代码,需要的朋友可以参考一下
代码如下:private void button1_Click(object sender, EventArgs e)&&&&&&& {&&&&&&&&&&& if (string.IsNullOrEmpty(this.textBox1.Text.Trim()))&&&&&&&&&&& {&&&&&&&&&&&&&&& MessageBox.Show("请输入需要转换的信息!");&&&&&&&&&&&&&&&&&&&&&&&&&& }
&&&&&&&&&&& string content = textBox1.T
&&&&&&&&&&& Hashtable hints= new Hashtable();&& &&&&&&&&&&& hints.Add(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);//纠错级别&&&&&&&&&&& hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码格式
&&&&&&&&&&& ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300, hints);&&&&&&&&&&& Bitmap bitmap = toBitmap(byteMatrix);&&&&&&&&&&& pictureBox1.Image =&&&&&&&&&&& SaveFileDialog sFD = new SaveFileDialog();&&&&&&&&&&& sFD.Filter = "*.png|*.png";&&&&&&&&&&& sFD.AddExtension =&&&&&&&&&&& try&&&&&&&&&&& {&&&&&&&&&&&&&&& if (sFD.ShowDialog() == DialogResult.OK)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);&&&&&&&&&&&&&&& }&&&&&&&&&&& }&&&&&&&&&&& catch (Exception ex)&&&&&&&&&&& {&&&&&&&&&&&&&&& MessageBox.Show(ex.Message);&&&&&&&&&&& }
&&&&&&& public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)&&&&&&& {&&&&&&&&&&& System.Drawing.Imaging.EncoderParameters eps = new System.Drawing.Imaging.EncoderParameters();&&&&&&&&&&& eps.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);&&&&&&&&&&& Bitmap bmap = toBitmap(matrix);&&&&&&&&&&& bmap.Save(file, format);&&&&&&& }&&&&&&& public static Bitmap toBitmap(ByteMatrix matrix)&&&&&&& {&&&&&&&&&&& int width = matrix.W&&&&&&&&&&& int height = matrix.H&&&&&&&&&&& Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);&&&&&&&&&&& for (int x = 0; x & x++)&&&&&&&&&&& {&&&&&&&&&&&&&&& for (int y = 0; y & y++)&&&&&&&&&&&&&&& {&&&&&&&&&&&&&&&&&&& bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("Purple") : ColorTranslator.FromHtml("0xFFFFFFFF"));//可以自定义颜色和背景色&&&&&&&&&&&&&&& }&&&&&&&&&&& }
&&&&&&&&&&&&&&& &&&&&&& }
您可能感兴趣的文章:
上一篇:下一篇:
最 近 更 新
热 点 排 行
12345678910& & 最近在研究二维码识别,用了Zxing的开源代码,但识别GBK类型老是出现乱码。折腾了两天,今天终于解决了,小记一下,。& & 我是在Zxing-1.6基础上开发的,因为zxing1.6对竖屏要支持好一些。& & 首先要搭建编译core.jar的环境,这个我就不多说了,不会的话可以参考& & 主要是改源码部分& & 修改core\src\com\google\zxing\common&StringUtils.java文件& & 1、 在private static final String ISO88591 = "ISO8859_1";下面一行添加& & [html]& & private static final String GBK = "GB2312";& & 2、在boolean canBeUTF8 =下面添加& & [html]& & boolean canBeGBK =& & 2、在 int value = bytes[i] & 0xFF;下面开始添加& & [html]& & //GBK stuff& & if (value > 0x7F)// 如果大于127,则可能是GB2312,就开始判断该字节,和下一个字节& & {& & if (value > 0xB0 && value <= 0xF7)// 第一个字节再此范围内,则开始判断第二个自己& & {& & int value2 = bytes[i + 1] & 0xFF;& & if (value2 > 0xA0 && value2 <= 0xF7)& & {& & canBeGBK =& & }& & }& & }& & 3、在 if (canBeShiftJIS && (maybeDoubleByteCount >= 3 || 20 * maybeSingleByteKatakanaCount > length)) {& & return SHIFT_JIS;& & }下面添加& & [html]& & if(canBeGBK){& & return GBK;& & }& & 再ant生产core.jar替换之前的就可以了,基本都是依葫芦画瓢,大家应该看得懂,& & 最后我把完整的StringUtils.java代码放上来,可以对比着看& & [html]& & /*& & * Copyright (C) 2010 ZXing authors& & *& & * Licensed under the Apache License, Version 2.0 (the "License");& & * you may not use this file except in compliance with the License.& & * You may obtain a copy of the License at& & *& & *&&&&&&&&* Unless required by applicable law or agreed to in writing, software& & * distributed under the License is distributed on an "AS IS" BASIS,& & * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.& & * See the License for the specific language governing permissions and& & * limitations under the License.& & */& & package com.& & import java.util.H& & import com.google.zxing.DecodeHintT& & /**& & * Common string-related functions.& & *& & * @author Sean Owen& & */& & public final class StringUtils {& & private static final String PLATFORM_DEFAULT_ENCODING =& & System.getProperty("file.encoding");& & public static final String SHIFT_JIS = "SJIS";& & private static final String EUC_JP = "EUC_JP";& & private static final String UTF8 = "UTF8";& & private static final String ISO88591 = "ISO8859_1";& & private static final String GBK = "GB2312";& & private static final boolean ASSUME_SHIFT_JIS =& & SHIFT_JIS.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING) ||& & EUC_JP.equalsIgnoreCase(PLATFORM_DEFAULT_ENCODING);& & private StringUtils() {}& & /**& & * @param bytes bytes encoding a string, whose encoding should be guessed& & * @param hints decode hints if applicable& & * @return name at the moment will only guess one of:& & *&{@link #SHIFT_JIS}, {@link #UTF8}, {@link #ISO88591}, or the platform& & *&default encoding if none of these can possibly be correct& & */& & public static String guessEncoding(byte[] bytes, Hashtable hints) {& & if (hints != null) {& & String characterSet = (String) hints.get(DecodeHintType.CHARACTER_SET);& & if (characterSet != null) {& & return characterS& & }& & }& & // Does it start with the UTF-8 byte order mark? then guess it&#39;s UTF-8& & if (bytes.length > 3 &&& & bytes[0] == (byte) 0xEF &&& & bytes[1] == (byte) 0xBB &&& & bytes[2] == (byte) 0xBF) {& & return UTF8;& & }& & // For now, merely tries to distinguish ISO-8859-1, UTF-8 and Shift_JIS,& & // which should be by far the most common encodings. ISO-8859-1& & // should not have bytes in the 0x80 - 0x9F range, while Shift_JIS& & // uses this as a first byte of a two-byte character. If we see this& & // followed by a valid second byte in Shift_JIS, assume it is Shift_JIS.& & // If we see something else in that second byte, we&#39;ll make the risky guess& & // that it&#39;s UTF-8.& & int length = bytes.& & boolean canBeISO88591 =& & boolean canBeShiftJIS =& & boolean canBeUTF8 =& & boolean canBeGBK =& & int utf8BytesLeft = 0;& & int maybeDoubleByteCount = 0;& & int maybeSingleByteKatakanaCount = 0;& & boolean sawLatin1Supplement =& & boolean sawUTF8Start =& & boolean lastWasPossibleDoubleByteStart =& & for (int i = 0;& & i < length && (canBeISO88591 || canBeShiftJIS || canBeUTF8 || canBeGBK);& & i++) {& & int value = bytes[i] & 0xFF;& & //GBK stuff& & if (value > 0x7F)// 如果大于127,则可能是GB2312,就开始判断该字节,和下一个字节& & {& & if (value > 0xB0 && value <= 0xF7)// 第一个字节再此范围内,则开始判断第二个自己& & {& & int value2 = bytes[i + 1] & 0xFF;& & if (value2 > 0xA0 && value2 <= 0xF7)& & {& & canBeGBK =& & }& & }& & }& & // UTF-8 stuff& & if (value >= 0x80 && value <= 0xBF) {& & if (utf8BytesLeft > 0) {& & utf8BytesLeft--;& & }& & } else {& & if (utf8BytesLeft > 0) {& & canBeUTF8 =& & }& & if (value >= 0xC0 && value <= 0xFD) {& & sawUTF8Start =& & int valueCopy =& & while ((valueCopy & 0x40) != 0) {& & utf8BytesLeft++;& & valueCopy <<= 1;& & }& & }& & }& & // ISO-8859-1 stuff& & if ((value == 0xC2 || value == 0xC3) && i < length - 1) {& & // This is really a poor hack. The slightly more exotic characters people might want to put in& & // a QR Code, by which I mean the Latin-1 supplement characters (e.g. u-umlaut) have encodings& & // that start with 0xC2 followed by [0xA0,0xBF], or start with 0xC3 followed by [0x80,0xBF].& & int nextValue = bytes[i + 1] & 0xFF;& & if (nextValue <= 0xBF &&& & ((value == 0xC2 && nextValue >= 0xA0) || (value == 0xC3 && nextValue >= 0x80))) {& & sawLatin1Supplement =& & }& & }& & if (value >= 0x7F && value <= 0x9F) {& & canBeISO88591 =& & }& & // Shift_JIS stuff& & if (value >= 0xA1 && value <= 0xDF) {& & // count the number of characters that might be a Shift_JIS single-byte Katakana character& & if (!lastWasPossibleDoubleByteStart) {& & maybeSingleByteKatakanaCount++;& & }& & }& & if (!lastWasPossibleDoubleByteStart &&& & ((value >= 0xF0 && value <= 0xFF) || value == 0x80 || value == 0xA0)) {& & canBeShiftJIS =& & }& & if (((value >= 0x81 && value = 0xE0 && value <= 0xEF))) {& & // These start double-byte characters in Shift_JIS. Let&#39;s see if it&#39;s followed by a valid& & // second byte.& & if (lastWasPossibleDoubleByteStart) {& & // If we just checked this and the last byte for being a valid double-byte& & // char, don&#39;t check starting on this byte. If this and the last byte& & // formed a valid pair, then this shouldn&#39;t be checked to see if it starts& & // a double byte pair of course.& & lastWasPossibleDoubleByteStart =& & } else {& & // ... otherwise do check to see if this plus the next byte form. a valid& & // double byte pair encoding a character.& & lastWasPossibleDoubleByteStart =& & if (i >= bytes.length - 1) {& & canBeShiftJIS =& & } else {& & int nextValue = bytes[i + 1] & 0xFF;& & if (nextValue
0xFC) {& & canBeShiftJIS =& & } else {& & maybeDoubleByteCount++;& & }& & // There is some conflicting information out there about which bytes can follow which in& & // double-byte Shift_JIS characters. The rule above seems to be the one that matches practice.& & }& & }& & } else {& & lastWasPossibleDoubleByteStart =& & }& & }& & if (utf8BytesLeft > 0) {& & canBeUTF8 =& & }& & // Easy -- if assuming Shift_JIS and no evidence it can&#39;t be, done& & if (canBeShiftJIS && ASSUME_SHIFT_JIS) {& & return SHIFT_JIS;& & }& & if (canBeUTF8 && sawUTF8Start) {& & return UTF8;& & }& & // Distinguishing Shift_JIS and ISO-8859-1 can be a little tough. The crude heuristic is:& & // - If we saw& & //&&- at least 3 bytes that starts a double-byte value (bytes that are rare in ISO-8859-1), or& & //&&- over 5% of bytes could be single-byte Katakana (also rare in ISO-8859-1),& & // - and, saw no sequences that are invalid in Shift_JIS, then we conclude Shift_JIS& & if (canBeShiftJIS && (maybeDoubleByteCount >= 3 || 20 * maybeSingleByteKatakanaCount > length)) {& & return SHIFT_JIS;& & }& & if(canBeGBK){& & return GBK;& & }& & // Otherwise, we default to ISO-8859-1 unless we know it can&#39;t be& & if (!sawLatin1Supplement && canBeISO88591) {& & return ISO88591;& & //&return&GB2312;& & }& & // Otherwise, we take a wild guess with platform. encoding& & return PLATFORM_DEFAULT_ENCODING;& & }& & }& & 再上一个效果对比图吧& & 识别的二维码图片是& & & & 解决前后的对比图& & 乱码& & 乱码& &
声明:该文章系网友上传分享,此内容仅代表网友个人经验或观点,不代表本网站立场和观点;若未进行原创声明,则表明该文章系转载自互联网;若该文章内容涉嫌侵权,请及时向
上一篇:下一篇:
相关经验教程
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.003 收益
的原创经验被浏览,获得 ¥0.003 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.003 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.005 收益
的原创经验被浏览,获得 ¥0.001 收益
的原创经验被浏览,获得 ¥0.001 收益

我要回帖

更多关于 ios zxing 生成二维码 的文章

 

随机推荐