Category: Snippets

Setting Manual Exposure in OpenCV

11 June, 2017 (14:01) | Snippets, Programming, Code, Python, OpenCV

In OpenCV, you can set the exposure for your camera manually by using the VideoCapture::set method. What isn’t obvious is what value to pass in for it. I have experimented with various webcams and the below settings seem to be accurate enough. Not all cameras allow setting the exposure programatically, so if […]

Android: Simple Example of Page Transition Using ViewPager

17 May, 2015 (18:54) | Java, Snippets, Programming, Code, Android

The ViewPager class built in to the Android SDK is a simple method of showing an animated transition between two views. For example, turning a page, or a calendar scrolling through months. This functionality can be added with only a few lines of code. Below I present an example containing pretty much […]

A Quick Snippet for Drawing Images in Java

2 October, 2014 (02:22) | Java, Snippets, Programming, Code, Android

Java does not have the best built-in tools in the world for drawing images, but it’s still useful for many purposes. Below is just a snippet on getting started drawing in Java. For more information on what’s available see the Java Graphics class reference.

BufferedImage i = new BufferedImage(500, 500,BufferedImage.TYPE_INT_RGB);
Graphics g=i.createGraphics();
g.drawLine(0, 0, 500, 500);
g.drawString(”This […]

Android: Accessing other views from inside a custom view using findViewById().

1 October, 2014 (11:15) | Java, Snippets, Programming, Android

If you attempt to call findViewById() on the ID of a view that is not from the Activity that holds the view, findViewById() returns null. I’ve seen a lot of solutions posted for attempting to access a View from outside the activity that created it. Most of them involve inflating the original view […]

Why you should return “this” from your setter methods.

26 September, 2014 (21:35) | .Net, Java, Snippets, Programming, C#, Code

Fortunately most API designers know to use this technique, but it’s still pretty common to find some that do not. It’s generally good practice, that when writing a setter method for a class, that you return the object’s “this” rather than “void”. The reason is that it allows a user of your class […]

Simple encryption and decryption of a string in c#

20 August, 2010 (12:32) | Cryptography, Security, Snippets, Programming, C#, Code

Here are some routines which are designed for simple use of Rijndael in C#. I’ve combined a test function in the class for simplicity of showing it’s use.

private static byte[] salt = Encoding.ASCII.GetBytes(”somerandomstuff”);

public static string Encrypt(string plainText, string […]

Simple speach example in C#.

12 May, 2008 (15:57) | .Net, Snippets

This uses reflection to avoid the clerical task of adding a project reference:

System.Type t = System.Type.GetTypeFromProgID(”SAPI.SpVoice”);
object o = System.Activator.CreateInstance(t);
t.InvokeMember(”Speak”, System.Reflection.BindingFlags.InvokeMethod, null, o, new object[] { “test”, 0 });

If you don’t mind adding a reference, then the whole thing can be done in one line:

new SpeechLib.SpVoice().Speak(”This is a test”, SpeechLib.SpeechVoiceSpeakFlags.SVSFDefault);

Finding “dead time” in a database of start and end times.

22 May, 2007 (15:28) | Snippets, SQL

The following snippet will find “dead time” (e.g. time where no events are scheduled) in a database:
1 select distinct dateadd(s,-1,starttime) as deadtime,"start" from sometable t where
2 0=(select count(*) from sometable u where u.starttime < t.deadtime and u.endtime > t.deadtime)
3 union all

MD5 in a few lines of Java

12 March, 2007 (10:58) | Cryptography, Java, Security, Snippets

1 import*;
2 import java.math.*;
4 public class MD5 {
5 public static void main(String args[]) throws Exception{
6 String s="This is a test";

Get the Unix Epoch time in one line of C#

3 January, 2007 (11:52) | .Net, Snippets

int epoch = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;

An alternate (possibly faster) method might be:
long time = (DateTime.UtcNow.Ticks - 621355968000000000) / 10000000;
Although the second method does conform to the Unmaintainable Code standard, I’d recommend sticking with the first method unless you really need those extra nanoseconds.