Java

Image previewer for Java

Posted in Development, Java on January 8th, 2011 by admin – Be the first to comment

At the moment I’m processing images in a CLI-Java-application. For debugging puposes it’s sometimes nice to have a way to display Image objects, like some kind of visual replacement for System.out.println(xyz).

This small method might be helpful in such cases:

public static void preview(Image image) {
	JFrame frame = new JFrame();
	JLabel label = new JLabel(new ImageIcon(image));
	frame.getContentPane().add(label, BorderLayout.CENTER);
	frame.pack();
	frame.setVisible(true);
}

Use Notifo’s API with Java

Posted in Development, Java, iPhone on January 8th, 2011 by admin – Be the first to comment

This is just a small hack to use Notifo’s API with Java:

URL url = new URL("https://api.notifo.com/v1/send_notification");
URLConnection connection = url.openConnection();
String authorizationString = "Basic " + new String(Base64.encodeBase64((username + ":" + apiSecret).getBytes()));
connection.setRequestProperty("Authorization", authorizationString);
connection.setDoOutput(true);
OutputStreamWriter output = new OutputStreamWriter(connection.getOutputStream());
output.write("to=m");
output.write("&msg="+URLEncoder.encode(msg,"UTF8"));
output.write("&uri="+URLEncoder.encode(uri,"UTF8"));
output.write("&label="+URLEncoder.encode(label,"UTF8"));
output.write("&title="+URLEncoder.encode(title,"UTF8"));
output.flush();
output.close();
connection.getInputStream().close();

(Remember: This is just a quick’n'dirty solution. It just works for me and is not especially beautiful)

This code depends on Apache Commons for base64-encoding.

Feel free to drop me a line, if this was helpful. The code is released under WTFPL ;-)

DHBW Connect – Autologin für das WLan der DHBW Mannheim

Posted in DHBW, Development, Java on December 5th, 2010 by admin – Be the first to comment

Letztes Woche ist es fertig geworden: “DHBW Connect” ist ein kleines Java-Programm, welches dich bei existierender WLan-Verbindung automatisch anmeldet. Du musst also nur noch ein einziges Mal deinen Benutzernamen und dein Passwort eingeben. (Wichtig: Nach Start der Datei passiert nicht viel – Es wird nur ein Trayicon neben der Uhr angezeigt. Um euer Passwort einzustellen genügt ein Rechtsklick|Konfigurieren auf dieses Icon.)

Netter Nebeneffekt: Durch den Verbindungscheck alle 10 Sekunden ist die Verbindung gefühlt ein wenig stabiler. Vielleicht ist das aber auch nur Einbildung… ;-)

Hier ist der Download. Wer mitentwickeln will oder eine intelligente Erweiterung hat, kann das Programm gerne bei Github forken.

Using the command line with java

Posted in Development, Java, Linux, Resourcen on September 20th, 2010 by admin – Be the first to comment

If you are looking for a simple way to use the command line via Java use this wrapper class. It offers a simple solution for a simple task. KISS!

How to use it:

System.out.println(new CommandLine("/var").exec("ls"));

And here is the class:

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
 
public class CommandLine {
 
	private String path ;
 
	public CommandLine() {
		path = "/";
	}
 
	public CommandLine(String path) {
		this.path = path;
	}
 
	public String getPath() {
		return path;
	}
 
	public void setPath(String path) {
		this.path = path;
	}
 
	public String exec(String cmd) {
		String s = null;
		String ret = "";
		File workDir = new File(path);
		try {
			Process p = Runtime.getRuntime().exec(cmd, null, workDir);
			int i = p.waitFor();
			if (i == 0){
				BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
				while ((s = stdInput.readLine()) != null) {
					ret = ret + s + "\n";
				}
			}
			else {
				BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
				while ((s = stdErr.readLine()) != null) {
					ret = ret + s + "\n";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
 
}