private static void imageWorker_DoWork(object sender, DoWorkEventArgs e) { // scanning for a barcode var wbmp = (WriteableBitmap)e.Argument; var luminiance = new RGBLuminanceSource(wbmp, wbmp.PixelWidth, wbmp.PixelHeight); var binarizer = new HybridBinarizer(luminiance); var binBitmap = new BinaryBitmap(binarizer); var reader = new MultiFormatReader(); e.Result = reader.decode(binBitmap); }
public string CodeDecoder(string path) { Bitmap bitMap; MemoryStream ms; WebClient Client = new WebClient(); string url = @"https://api.telegram.org/file/bot649665785:AAF3lm9sagdFTtiF4t5p0uBhwr1PhabLSCs/"; url = url + path; try { System.Net.WebRequest request = System.Net.WebRequest.Create( url); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); bitMap = new Bitmap(responseStream); ms = new MemoryStream(); //实例化内存流 bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //把位图信息保存到内存流里面 byte[] bytes = ms.GetBuffer(); //把颜色信息转化为byte数据 LuminanceSource source = new RGBLuminanceSource(bytes, bitMap.Width, bitMap.Height); //得到位图的像素数值内容 BinaryBitmap bb = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); //处理像素值内容信息 MultiFormatReader mutireader = new ZXing.MultiFormatReader(); //实例化MultiFormatReader Result str = mutireader.decode(bb); //通过mutireader.decode()得到解析后的结果 ms.Close(); //关闭内存流 if (str == null) { return(null); } else { return(str.Text);//返回解析结果 } } catch (System.Net.WebException e) { return(e.ToString()); } }
//private string save_path = @"d:/"; public string CodeDecoder(string path) { http = http + path; try { System.Net.WebRequest request = System.Net.WebRequest.Create( http); System.Net.WebResponse response = request.GetResponse(); System.IO.Stream responseStream = response.GetResponseStream(); bitMap = new System.Drawing.Bitmap(responseStream); } catch (System.Net.WebException) { //MessageBox.Show("There was an error opening the image file." // + "Check the URL"); } //bitMap = new Bitmap(http); MemoryStream ms = new MemoryStream(); //实例化内存流 bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); //把位图信息保存到内存流里面 byte[] bytes = ms.GetBuffer(); //把颜色信息转化为byte数据 LuminanceSource source = new RGBLuminanceSource(bytes, bitMap.Width, bitMap.Height); //得到位图的像素数值内容 BinaryBitmap bb = new BinaryBitmap(new ZXing.Common.HybridBinarizer(source)); //处理像素值内容信息 MultiFormatReader mutireader = new ZXing.MultiFormatReader(); //实例化MultiFormatReader Result str = mutireader.decode(bb); //通过mutireader.decode()得到解析后的结果 if (str == null) { return(null); } return(str.Text); //返回解析结果 ms.Close(); //关闭内存流 }
void Update() { if (!e_DeviceController.isPlaying ) { return; } if (e_DeviceController.isPlaying && !decoding) { orginalc = e_DeviceController.cameraTexture.GetPixels32(); W = e_DeviceController.cameraTexture.width; H = e_DeviceController.cameraTexture.height; WxH = W * H; targetbyte = new byte[ WxH ]; z = 0; // convert the image color data for(int y = H - 1; y >= 0; y--) { for(int x = 0; x < W; x++) { // targetbyte[z++] = (byte)( (((int)orginalc[y * W + x].r)+ ((int)orginalc[y * W + x].g) + ((int)orginalc[y * W + x].b))/3); targetbyte[z++] = (byte)(((int)orginalc[y * W + x].r)<<16 | ((int)orginalc[y * W + x].g)<<8 | ((int)orginalc[y * W + x].b)); } } Loom.RunAsync(() => { try { RGBLuminanceSource luminancesource = new RGBLuminanceSource(targetbyte, W, H, true); var bitmap = new BinaryBitmap(new HybridBinarizer(luminancesource.rotateCounterClockwise())); Result data; var reader = new MultiFormatReader(); data = reader.decode(bitmap); if (data != null) { { decoding = true; dataText = data.Text; } } else { for(int y = 0; y != targetbyte.Length; y++) { targetbyte[y] = (byte) ( 0xff - (targetbyte[y] & 0xff)); } luminancesource = new RGBLuminanceSource(targetbyte, W, H, true); bitmap = new BinaryBitmap(new HybridBinarizer(luminancesource)); data = reader.decode(bitmap); if (data != null) { { decoding = true; dataText = data.Text; } } } } catch (Exception e) { decoding = false; } }); } if(decoding) { if(tempDecodeing != decoding) { e_QRScanFinished(dataText);//triger the sanfinished event; } tempDecodeing = decoding; } }
private void btnSave_Click(object sender, EventArgs e) { var reader = new MultiFormatReader(); var img = (Bitmap)Bitmap.FromFile(pictureBox1.ImageLocation); BitmapLuminanceSource ls = new BitmapLuminanceSource(img); var binarizer = new HybridBinarizer(ls); BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer); var result = reader.decode(binaryBitmap); richTextBox1.Text = result.Text.Trim(); }