Posted By

nausherwan on 05/25/14


Tagged

cloud barcode Aspose asposebarcode


Versions (?)

Sample Code to Create Barcode and Get Image as Stream using Aspose.BarCode for Cloud API


 / Published in: C#
 

URL: http://www.aspose.com/docs/display/barcodecloud/Create+Barcode+and+Get+Image+as+Stream

The following code sample shows how developers can create a barcode and get the image file using Aspose.BarCode for Cloud API in their applications. Developers can use Aspose REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more.

  1. string appSID = "******************";
  2. string appKey = "******************";
  3.  
  4.  
  5. //type: Codabar, Code11, Code128 and Code39Extended etc.
  6. //format: png, jpeg and jpg etc.
  7. string barcodeText = "Aspose";
  8. string type = "DataMatrix";
  9. string format = "png";
  10.  
  11. //build URI to generate barcode in the Aspose cloud storage
  12. string strURI = "http://api.aspose.com/v1.1/barcode/generate?text="+ barcodeText +"&type="+ type +"&format=" + format;
  13.  
  14. //sign the remote URI
  15. string signedURI = Sign(strURI, appSID, appKey);
  16.  
  17. // Send the request to Aspose cloud server
  18. Stream responseStream = ProcessCommand(signedURI, "GET");
  19.  
  20. // Read the response, in this case the response is a Stream that contains barcode image
  21. // Just copy the response stream to the image stream that user passed
  22. using (Stream fileStream = System.IO.File.OpenWrite("C:\\Temp\\output.png"))
  23. {
  24. CopyStream(responseStream, fileStream);
  25. }
  26.  
  27. // close the response stream
  28. responseStream.Close();
  29.  
  30.  
  31. /// <summary>
  32. /// Copies the contents of input to output. Doesn't close either stream.
  33. /// </summary>
  34. public static void CopyStream(Stream input, Stream output)
  35. {
  36. try
  37. {
  38. byte[] buffer = new byte[8 * 1024];
  39. int len;
  40. while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
  41. {
  42. output.Write(buffer, 0, len);
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. throw new Exception(ex.Message);
  48. }
  49. }
  50.  
  51. /// <summary>
  52. /// Method to Sign your app key
  53. /// This should be called with each URI resource
  54. /// before sending any HTTP command to the remote server
  55. /// </summary>
  56. public static string Sign(string url, string AppSID, string AppKey)
  57. {
  58. try
  59. {
  60. // Add appSID parameter.
  61. UriBuilder builder = new UriBuilder(url);
  62. if (builder.Query != null && builder.Query.Length > 1)
  63. builder.Query = builder.Query.Substring(1) + "&appSID=" + AppSID;
  64. else
  65. builder.Query = "appSID=" + AppSID;
  66. // Remove final slash here as it can be added automatically.
  67. builder.Path = builder.Path.TrimEnd('/');
  68. // Compute the hash.
  69.  
  70. byte[] privateKey = System.Text.Encoding.UTF8.GetBytes(AppKey);
  71.  
  72. System.Security.Cryptography.HMACSHA1 algorithm = new System.Security.Cryptography.HMACSHA1(privateKey);
  73. //System.Text.ASCIIEncoding
  74. byte[] sequence = System.Text.ASCIIEncoding.ASCII.GetBytes(builder.Uri.AbsoluteUri);
  75. byte[] hash = algorithm.ComputeHash(sequence);
  76. string signature = Convert.ToBase64String(hash);
  77. // Remove invalid symbols.
  78. signature = signature.TrimEnd('=');
  79. signature = System.Web.HttpUtility.UrlEncode(signature);
  80. // Convert codes to upper case as they can be updated automatically.
  81. signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", e => e.Value.ToUpper());
  82. //signature = System.Text.RegularExpressions.Regex.Replace(signature, "%[0-9a-f]{2}", delegate(string e){ e.Value.ToUpper()});
  83. // Add the signature to query string.
  84. return string.Format("{0}&signature={1}", builder.Uri.AbsoluteUri, signature);
  85. }
  86. catch (Exception ex)
  87. {
  88. throw new Exception(ex.Message);
  89. }
  90. }
  91.  
  92. public static Stream ProcessCommand(string strURI, string strHttpCommand)
  93. {
  94. try
  95. {
  96. Uri address = new Uri(strURI);
  97. System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(address);
  98. request.Method = strHttpCommand;
  99. request.ContentType = "application/json";
  100.  
  101. request.ContentLength = 0;
  102. System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
  103. return response.GetResponseStream();
  104. }
  105. catch (System.Net.WebException webex)
  106. {
  107. throw new Exception(webex.Message);
  108. }
  109. catch (Exception Ex)
  110. {
  111. throw new Exception(Ex.Message);
  112. }
  113. }

Report this snippet  

You need to login to post a comment.