Skip to content

Instantly share code, notes, and snippets.

@codec-abc
Created August 24, 2017 13:21
Show Gist options
  • Select an option

  • Save codec-abc/2d3cb841336daf565f529476c07dc3bb to your computer and use it in GitHub Desktop.

Select an option

Save codec-abc/2d3cb841336daf565f529476c07dc3bb to your computer and use it in GitHub Desktop.
Test Lib Camera Rust
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApp3
{
class Program
{
const string dllPath = @"C:\DEV\Escapi\escapi\target\debug\escapi_rust.dll";
[STAThread]
static int Main(string[] args)
{
Int64 device_ptr;
uint camera_index = 0;
uint width = 320;
uint height = 240;
float framerate = 30.0f;
var result = init(camera_index, width, height, framerate, out device_ptr);
if (result != 0)
{
Int64 str_ptr;
result = get_device_name(device_ptr, out str_ptr);
if (result != 0)
{
byte[] bytes = new byte[result];
Marshal.Copy(new IntPtr(str_ptr), bytes, 0, result);
var deviceName = Encoding.UTF8.GetString(bytes);
Console.WriteLine("device name is " + deviceName);
free_string(str_ptr);
}
Int64 bufferPtr;
Int64 bufferLength;
result = get_capture_buffer(device_ptr, out bufferPtr, out bufferLength);
if (result != 0)
{
byte[] imageBytes = new byte[bufferLength * 4];
Marshal.Copy(new IntPtr(bufferPtr), imageBytes, 0, (int) (bufferLength * 4));
SavePPM(width, height ,imageBytes);
}
free_device(device_ptr);
}
Console.WriteLine("Program ended. Press a key to exit.");
Console.ReadLine();
return 0;
}
private static void SavePPM(uint width, uint height, byte[] imageBytes)
{
var imageBytesLength = imageBytes.Length;
using (var writer = new StreamWriter("image.ppm"))
{
writer.WriteLine("P3");
writer.WriteLine("" + width + " " + height);
writer.WriteLine("255");
for (int j = 0; j < height; j++)
{
for (int i = 0; i < width ; i++)
{
for (int channel = 0; channel < 4; channel++)
{
if (channel < 3)
{
var baseIndex = j * width * 4 + i * 4;
var swapedChannelIndx = SwapChannelIndex(channel);
var index = baseIndex + swapedChannelIndx;
writer.Write("" + imageBytes[index] + " ");
}
}
}
writer.WriteLine("");
}
}
}
private static int SwapChannelIndex(int channel)
{
if (channel == 0)
{
return 2;
}
if (channel == 2)
{
return 0;
}
return channel;
}
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern Int32 init
(
UInt32 camIndex,
UInt32 width,
UInt32 height,
float desired_fps,
out Int64 intPtr
);
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern Int32 num_devices();
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern Int32 get_device_name(Int64 device_ptr, out Int64 str_ptr_out);
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern void free_string(Int64 str_ptr);
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern void free_device(Int64 str_ptr);
[DllImport(dllPath, CharSet = CharSet.Auto)]
public static extern Int32 get_capture_buffer(Int64 device_ptr, out Int64 buffer_ptr_out, out Int64 buffer_length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment