Skip to content

Add example, add new type message and fixed bug for .net462 #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
140 changes: 140 additions & 0 deletions Samples/StreamingCam/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
using System;
using System.Diagnostics;
using System.Threading;
using Uml.Robotics.Ros;
using std_msgs = Messages.std_msgs;
using Image = Messages.sensor_msgs.Image;
using System.Drawing;
using System.Drawing.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
using System.Linq;
using System.IO;
using System.Runtime.InteropServices;

namespace StreamingCam
{
class Program
{
static Bitmap video;
static void Main(string[] args)
{


FilterInfoCollection VideoCaptureDevices;
VideoCaptureDevice FinalVideo;



Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
ROS.Init(args, "Talker");
var spinner = new SingleThreadSpinner();
NodeHandle node = new NodeHandle();

Publisher<Image> Talker = node.Advertise<Image>("/video", 1);
int count = 0;



VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);
FinalVideo.Start();



while (ROS.OK && !Console.KeyAvailable)
{
Console.WriteLine("publishing message");
ROS.Info()("Publishing a chatter message: \"Blah blah blah " + "");

if (video != null)
{
Image img = new Image();
img.header = new std_msgs.Header();
img.header.seq = (uint)count++;
img.header.stamp = new std_msgs.Time();
img.header.frame_id = "/jkfd";
img.width = (uint)video.Width;
img.height = (uint)video.Height;
img.encoding = "rgb8";
img.is_bigendian = 0;
img.step = 640 * 1 * 3;
byte[] tmp = BitmapToByteArray(video);

//img.data = new byte[img.width * img.height * 3];
//Array.Copy(tmp, 54, img.data, 0, tmp.Length - 54);
img.data = BitmapToByteArray(video);
//323704
//921654

Talker.Publish(img);
spinner.SpinOnce();
}
Thread.Sleep(300);
}
ROS.Shutdown();

//view v = new view();
//v.ShowDialog();



}

static void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();
//Bitmap v= new Bitmap()
//this.pictureBox1.Image = video;





}

public static byte[] BitmapToByteArray(Bitmap bitmap)
{

BitmapData bmpdata = null;

try
{
bmpdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
int numbytes = bmpdata.Stride * bitmap.Height;
byte[] bytedata = new byte[numbytes];
IntPtr ptr = bmpdata.Scan0;

Marshal.Copy(ptr, bytedata, 0, numbytes);

return bytedata;
}
finally
{
if (bmpdata != null)
bitmap.UnlockBits(bmpdata);
}

}

static byte[] ConvertBitMapToByteArray(Bitmap bitmap)
{

bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone); // in ros lo 0:0 si trova al top left

MemoryStream stream = new MemoryStream();
bitmap.Save(stream, ImageFormat.Bmp);
return stream.ToArray();



}
public static byte[] ImageToByte(Bitmap img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
}
}
35 changes: 35 additions & 0 deletions Samples/StreamingCam/StreamingCam.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net462</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Uml.Robotics.Ros.Messages\Uml.Robotics.Ros.Messages.csproj" />
<ProjectReference Include="..\..\Uml.Robotics.Ros\Uml.Robotics.Ros.csproj" />
</ItemGroup>

<ItemGroup>
<Reference Include="AForge">
<HintPath>..\..\..\..\Git_repo\TPV_Aena\TPV C sharp\lib\AForge.dll</HintPath>
</Reference>
<Reference Include="AForge.Imaging">
<HintPath>..\..\..\..\Git_repo\TPV_Aena\TPV C sharp\lib\AForge.Imaging.dll</HintPath>
</Reference>
<Reference Include="AForge.Video">
<HintPath>..\..\..\..\Git_repo\TPV_Aena\TPV C sharp\lib\AForge.Video.dll</HintPath>
</Reference>
<Reference Include="AForge.Video.DirectShow">
<HintPath>..\..\..\..\Git_repo\TPV_Aena\TPV C sharp\lib\AForge.Video.DirectShow.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup>
<Compile Update="view.cs">
<SubType>Form</SubType>
</Compile>
</ItemGroup>

</Project>
77 changes: 77 additions & 0 deletions Samples/StreamingCam/view.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions Samples/StreamingCam/view.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge.Video;
using AForge.Video.DirectShow;

namespace StreamingCam
{
public partial class view: Form
{
private FilterInfoCollection VideoCaptureDevices;
private VideoCaptureDevice FinalVideo;

public Bitmap video;
public view()
{
InitializeComponent();

VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

FinalVideo = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
FinalVideo.NewFrame += new NewFrameEventHandler(FinalVideo_NewFrame);






}

public void btnOK_Click(object sender, EventArgs e)
{
FinalVideo.Start();
}
private void view_FormClosing(object sender, FormClosingEventArgs e)
{
FinalVideo.Stop();
}
void FinalVideo_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
video = (Bitmap)eventArgs.Frame.Clone();

this.pictureBox1.Image = video;

}
}
}
15 changes: 8 additions & 7 deletions Uml.Robotics.Ros.MessageBase/TypeRegistryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;

#if NETCORE
using System.Runtime.Loader;
#endif
namespace Uml.Robotics.Ros
{
public class TypeRegistryBase
Expand Down Expand Up @@ -48,18 +50,17 @@ public static IEnumerable<Assembly> GetCandidateAssemblies(params string[] tagAs
#if NETCORE
var context = DependencyContext.Load(Assembly.GetEntryAssembly());
var loadContext = AssemblyLoadContext.Default;

return context.RuntimeLibraries
.Where(x => x.Dependencies.Any(d => referenceAssemblies.Contains(d.Name)))
.SelectMany(x => x.GetDefaultAssemblyNames(context))
.Select(loadContext.LoadFromAssemblyName);
#else
var context = DependencyContext.Load(Assembly.GetEntryAssembly());

Assembly[] a = AppDomain.CurrentDomain.GetAssemblies();

return context.RuntimeLibraries
.Where(x => x.Dependencies.Any(d => referenceAssemblies.Contains(d.Name)))
.SelectMany(x => x.GetDefaultAssemblyNames(context))
.Select(Assembly.Load);
return a.Where(x=> x.GetReferencedAssemblies().Any(y => referenceAssemblies.Contains(y.Name)));

#endif
}
}
Expand Down
7 changes: 7 additions & 0 deletions Uml.Robotics.Ros.sln
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ActionServerSample", "Sampl
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PubSubTestbed", "Tests\PubSubTestbed\PubSubTestbed.csproj", "{1C8D0504-5236-4FFF-AA8F-2CFF6C564DC0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StreamingCam", "Samples\StreamingCam\StreamingCam.csproj", "{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -115,6 +117,10 @@ Global
{1C8D0504-5236-4FFF-AA8F-2CFF6C564DC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C8D0504-5236-4FFF-AA8F-2CFF6C564DC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C8D0504-5236-4FFF-AA8F-2CFF6C564DC0}.Release|Any CPU.Build.0 = Release|Any CPU
{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -131,6 +137,7 @@ Global
{4F0E93D5-6CC7-4971-BB91-A5139CF4B05E} = {9C75EAAD-EACA-4E4F-B9DA-D7BADB3B7577}
{3041CF5C-222C-412E-8EE6-289C1DE0E567} = {9C75EAAD-EACA-4E4F-B9DA-D7BADB3B7577}
{1C8D0504-5236-4FFF-AA8F-2CFF6C564DC0} = {EEE8759B-47AD-455B-8F32-0A36EAE2512B}
{BBBE254B-D17E-45A8-B97E-1FDA4D9DD0E5} = {9C75EAAD-EACA-4E4F-B9DA-D7BADB3B7577}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {23E67514-C5C2-464B-8371-A9A618DE03BA}
Expand Down
Loading