public static Image GetQRCode(byte[] data, ErrorCorrectionLevel level = ErrorCorrectionLevel.M) { bool requires16BitLength = false; int maxBytesInVersion9Code = QRErrorCorrections.GetQRVersionInfo(9).GetCorrectionInfo(level).TotalDataBytes; if (maxBytesInVersion9Code - 2 < data.Length) { // This data requires a version 10 or higher code; will not fit in version 9 or lower. // Version 10 and higher codes require 16-bit data lengths. requires16BitLength = true; } StreamHelper sh = new StreamHelper(); sh.WriteNibble(0x04); // byte mode if (requires16BitLength) { sh.WriteWord((ushort)data.Length); } else { sh.WriteByte((byte)data.Length); } sh.WriteBytes(new ArraySegment <byte>(data)); sh.WriteNibble(0x00); // terminator byte[] binaryData = sh.ToArray(); int qrCodeVersion; ErrorCorrectionLevel errorCorrectionLevel; byte[] finalMessageSequence = QRErrorCorrections.GetMessageSequence(binaryData, level, out qrCodeVersion, out errorCorrectionLevel); SymbolTemplate template = SymbolTemplate.CreateTemplate(qrCodeVersion); template.ErrorCorrectionLevel = errorCorrectionLevel; template.PopulateData(finalMessageSequence); template.Complete(); return(template.ToImage()); }
public static Image GetQRCode(byte[] data, ErrorCorrectionLevel level = ErrorCorrectionLevel.M) { bool requires16BitLength = false; int maxBytesInVersion9Code = QRErrorCorrections.GetQRVersionInfo(9).GetCorrectionInfo(level).TotalDataBytes; if (maxBytesInVersion9Code - 2 < data.Length) { // This data requires a version 10 or higher code; will not fit in version 9 or lower. // Version 10 and higher codes require 16-bit data lengths. requires16BitLength = true; } StreamHelper sh = new StreamHelper(); sh.WriteNibble(0x04); // byte mode if (requires16BitLength) { sh.WriteWord((ushort)data.Length); } else { sh.WriteByte((byte)data.Length); } sh.WriteBytes(new ArraySegment<byte>(data)); sh.WriteNibble(0x00); // terminator byte[] binaryData = sh.ToArray(); int qrCodeVersion; ErrorCorrectionLevel errorCorrectionLevel; byte[] finalMessageSequence = QRErrorCorrections.GetMessageSequence(binaryData, level, out qrCodeVersion, out errorCorrectionLevel); SymbolTemplate template = SymbolTemplate.CreateTemplate(qrCodeVersion); template.ErrorCorrectionLevel = errorCorrectionLevel; template.PopulateData(finalMessageSequence); template.Complete(); return template.ToImage(); }
/// <summary> /// Called after an inbound message has been received but before the message is dispatched to the intended operation. /// </summary> /// <param name="request">The request message.</param> /// <param name="channel">The incoming channel.</param> /// <param name="instanceContext">The current service instance.</param> /// <returns>The object used to correlate state. This object is passed back in the <see cref="M:System.ServiceModel.Dispatcher.IDispatchMessageInspector.BeforeSendReply(System.ServiceModel.Channels.Message@,System.Object)" /> method.</returns> public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) { if (!request.Properties.ContainsKey("Via")) { return(request); // Nothing much we can do here } if (!request.Properties.ContainsKey("httpRequest")) { return(request); // Same here } var httpRequest = request.Properties["httpRequest"] as HttpRequestMessageProperty; if (httpRequest == null) { return(request); } var httpMethod = httpRequest.Method.ToUpper(); var uri = request.Properties["Via"] as Uri; if (uri == null) { return(request); // Still nothing much we can do } var url = uri.AbsoluteUri; var urlFragment = url; if (urlFragment.ToLower().StartsWith(_rootUrlLower)) { urlFragment = urlFragment.Substring(_rootUrlLower.Length); } var operationInfo = RestHelper.GetMethodNameFromUrlFragmentAndContract(urlFragment, httpMethod, _contractType); var urlParameters = RestHelper.GetUrlParametersFromUrlFragmentAndContract(urlFragment, httpMethod, _contractType); if (httpMethod == "GET") { // TODO: Support GET if at all possible throw new Exception("REST-GET operations are not currently supported in the chosen hosting environment. Please use a different HTTP Verb, or host in a different environment (such as WebApi). We hope to add this feature in a future version."); // This is a REST GET operation. Therefore, there is no posted message. Instead, we have to decode the input parameters from the URL var parameters = operationInfo.GetParameters(); if (parameters.Length != 1) { throw new NotSupportedException("Only service methods/operations with a single input parameter can be mapped to REST-GET operations. Method " + operationInfo.Name + " has " + parameters.Length + " parameters. Consider changing the method to have a single object with multiple properties instead."); } var parameterType = parameters[0].ParameterType; var parameterInstance = Activator.CreateInstance(parameterType); foreach (var propertyName in urlParameters.Keys) { var urlProperty = parameterType.GetProperty(propertyName); if (urlProperty == null) { continue; } urlProperty.SetValue(parameterInstance, urlParameters[propertyName], null); } // Seralize the object back into a new request message // TODO: We only need to do this for methods OTHER than GET var format = GetMessageContentFormat(request); switch (format) { case WebContentFormat.Xml: var xmlStream = new MemoryStream(); var xmlSerializer = new DataContractSerializer(parameterInstance.GetType()); xmlSerializer.WriteObject(xmlStream, parameterInstance); var xmlReader = XmlDictionaryReader.CreateTextReader(StreamHelper.ToArray(xmlStream), XmlDictionaryReaderQuotas.Max); var newXmlMessage = Message.CreateMessage(xmlReader, int.MaxValue, request.Version); newXmlMessage.Properties.CopyProperties(request.Properties); newXmlMessage.Headers.CopyHeadersFrom(request.Headers); if (format == WebContentFormat.Default) { if (newXmlMessage.Properties.ContainsKey(WebBodyFormatMessageProperty.Name)) { newXmlMessage.Properties.Remove(WebBodyFormatMessageProperty.Name); } newXmlMessage.Properties.Add(WebBodyFormatMessageProperty.Name, WebContentFormat.Xml); } request = newXmlMessage; break; case WebContentFormat.Default: case WebContentFormat.Json: var jsonStream = new MemoryStream(); var serializer = new DataContractJsonSerializer(parameterInstance.GetType()); serializer.WriteObject(jsonStream, parameterInstance); var jsonReader = JsonReaderWriterFactory.CreateJsonReader(StreamHelper.ToArray(jsonStream), XmlDictionaryReaderQuotas.Max); var newMessage = Message.CreateMessage(jsonReader, int.MaxValue, request.Version); newMessage.Properties.CopyProperties(request.Properties); newMessage.Headers.CopyHeadersFrom(request.Headers); if (format == WebContentFormat.Default) { if (newMessage.Properties.ContainsKey(WebBodyFormatMessageProperty.Name)) { newMessage.Properties.Remove(WebBodyFormatMessageProperty.Name); } newMessage.Properties.Add(WebBodyFormatMessageProperty.Name, WebContentFormat.Json); } request = newMessage; break; default: throw new NotSupportedException("Mesage format " + format.ToString() + " is not supported form REST/JSON operations"); } } return(null); }