Created
July 5, 2014 04:30
-
-
Save muhammadyaseen/2240e21b3fd5798663bb to your computer and use it in GitHub Desktop.
Working with HEX data (jpeg image) coming from Arduino Serial port
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Drawing; | |
using System.IO; | |
namespace JPEGProcess | |
{ | |
/// <summary> | |
/// This program writes the HEX representation of a JPEG image to the Console Window | |
/// Image path is hardcoded (bad thing!) | |
/// | |
/// The Line is broken after 15 HEX chars to enhance readability | |
/// | |
/// The Start Of Image 'FF D8' and End of Image 'FF D9' can be seen on Console | |
/// </summary> | |
/// | |
/// <author> | |
/// Muhammad Yaseen | |
/// </author> | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
/* Console.WriteLine("Select : "); | |
Console.WriteLine("1. Create packets from existing JPEG "); | |
Console.WriteLine("2. Create JPEG from packets "); | |
string choice = Console.ReadLine(); | |
if (choice == "1") | |
{ | |
createPackets(@"E:\created.jpg"); | |
} | |
if (choice == "2") | |
{ | |
reconstructImageFromPackets(@"E:\packets"); | |
}*/ | |
//call function as required | |
hextoJPEG(@"D:\yasir.txt"); | |
} | |
private static void reconstructImageFromPackets(string packetPath) | |
{ | |
//get all files under the 'packetPath' directory, each file represents a 32-bit data packet. | |
string[] camdataFiles = Directory.GetFiles(packetPath, "*.camdata").ToArray(); //10 packets for testing purpose | |
List<byte> imageBytes = new List<byte>(); | |
byte[] imageBytesArray = new byte[camdataFiles.Length * 2]; | |
foreach (string file in camdataFiles) | |
{ | |
//we have to read each data packet, remove unecessary data, and reassemble the image | |
Console.WriteLine("Reading : " + file); | |
BinaryReader camdataReader = new BinaryReader(File.OpenRead(file)); | |
short serialNumber = camdataReader.ReadInt16(); //first two bytes are serial | |
Console.WriteLine( "This is packet number : " + serialNumber); | |
byte[] imageData = new byte[2]; //next two bytes are image data | |
camdataReader.Read(imageData, 0, 2); | |
string packetContents = BitConverter.ToString(imageData); | |
Console.WriteLine("Packet Contents : {0}", packetContents.Replace('-', ' ')); | |
imageBytes.AddRange(imageData); | |
//add bytes to array in proper location | |
//we need to store the image data read so far, and when all files have been read, we write the read data to a single file. | |
imageBytesArray[serialNumber * 2 - 2] = imageData[0]; | |
imageBytesArray[serialNumber * 2 - 1] = imageData[1]; | |
Console.WriteLine(); | |
} | |
Console.WriteLine("==== packet Hex ====="); | |
showHEXofByteData(imageBytesArray); | |
Console.WriteLine("==== direct Hex ====="); | |
showHEXofJPEG(@"E:\created.jpg"); | |
Console.WriteLine("Writing packets to image file"); | |
BinaryWriter jpegWriter = new BinaryWriter(File.Open(@"E:\reassembled\output.jpg", FileMode.Create)); | |
jpegWriter.Write(imageBytesArray.ToArray()); | |
jpegWriter.Flush(); | |
jpegWriter.Close(); | |
} | |
private static void createPackets(string fileName) | |
{ | |
/* Packet structure: | |
* | |
* First 2 bytes are for serial number. | |
* Last two bytes are image data | |
* | |
* Packet: | |
* +===========================================+ | |
* | | | | |
* | 2 byte serial | 2 bytes image data | | |
* | | | | |
* +===========================================+ | |
*/ | |
Stream jpegStream = File.OpenRead(fileName); | |
ushort readBytes = 0; | |
ushort packetsCreated = 0; | |
while ( readBytes < jpegStream.Length ) | |
{ | |
//create a packet | |
Stream packet = File.Open(@"E:\packets\packet-" + ++packetsCreated + ".camdata", FileMode.Create); | |
//and a writerm which will write data to that packet | |
BinaryWriter packetWriter = new BinaryWriter(packet); | |
//first 2-bytes is a 'ushort' type designating the Serial Number | |
packetWriter.Write(packetsCreated); | |
//Next two bytes are Actual Image Data | |
byte[] imageBytes = new byte[2]; | |
jpegStream.Read(imageBytes, 0 , 2); | |
packetWriter.Write(imageBytes); | |
packetWriter.Flush(); | |
//packetWriter.Close(); | |
packet.Flush(); | |
//packet.Close(); | |
readBytes += 2; | |
} | |
jpegStream.Close(); | |
} | |
private static void showHEXofJPEG(string fileName) | |
{ | |
//Get the hex representation of specified image | |
var converted = BitConverter.ToString(File.ReadAllBytes(fileName)); | |
//remove the '-' character inserted by ToString() | |
List<string> lines = converted.Split(new char[] { '-' }).ToList(); | |
string line = String.Empty; | |
//print in lines of 15 HEX chars | |
for (int i = 0; i < lines.Count(); i++) | |
{ | |
Console.Write(lines[i] + " "); | |
if ((i + 1) % 15 == 0) Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
private static void showHEXofByteData(byte[] bytes) | |
{ | |
//Get the hex representation of specified image | |
var converted = BitConverter.ToString(bytes); | |
//remove the '-' character inserted by ToString() | |
List<string> lines = converted.Split(new char[] { '-' }).ToList(); | |
string line = String.Empty; | |
//print in lines of 15 HEX chars | |
for (int i = 0; i < lines.Count(); i++) | |
{ | |
Console.Write(lines[i] + " "); | |
if ((i + 1) % 15 == 0) Console.WriteLine(); | |
} | |
Console.WriteLine(); | |
} | |
private static void hextoJPEG(string fileName) | |
{ | |
// Some junk hex image data | |
string[] hexImgData = File.ReadAllText(fileName).Split('\r'); | |
//LINQ | |
var lines = hexImgData.Select(e => e.TrimStart('\n')); | |
string somehting= String.Empty; | |
foreach (string s in lines) | |
{ | |
somehting += s; | |
} | |
// Call function to Convert the hex data to byte array | |
byte[] newByte = ToByteArray(somehting); | |
MemoryStream memStream = new MemoryStream(newByte); | |
// Save the memorystream to file | |
Bitmap.FromStream(memStream).Save("e:\\testimg3_5.jpg"); | |
} | |
// Function converts hex data into byte array | |
public static byte[] ToByteArray(String HexString) | |
{ | |
int NumberChars = HexString.Length; | |
byte[] bytes = new byte[NumberChars / 2]; | |
for (int i = 0; i < NumberChars; i += 2) | |
{ | |
bytes[i/2] = Convert.ToByte(HexString.Substring(i, 2), 16); | |
} | |
return bytes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment