Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 11-20 of 21 total

Java - Brightness

public BufferedImage brightness(BufferedImage bi, float value)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(1, 1, new float[] {value});
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}

Java - ScreenShot

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

public class screen2image
{
	public static void main(String[] args) throws Exception
	{
		Robot robot = new Robot();
		
		BufferedImage screenShot = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		ImageIO.write(screenShot, "JPG", new File("screenShot.jpg"));
	}
}

Java - ImageFilter Simple

// Filtro Embrossing
	public BufferedImage Embrossing(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-2f, 0f, 0f, 
														 0f, 1f, 0f, 
														 0f, 0f, 2f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}


// Filtro Blurring
	public BufferedImage Blurring(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														1f/9f, 1f/9f, 1f/9f, 
														1f/9f, 1f/9f, 1f/9f, 
														1f/9f, 1f/9f, 1f/9f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}


// Filtro Sharpening
	public BufferedImage Sharpening(BufferedImage bi)
	{
		BufferedImage buff = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
		
		Kernel kernel = new Kernel(3, 3, new float[] {
														-1f, -1f, -1f, 
														-1f,  9f, -1f, 
														-1f, -1f, -1f
													 });
		
		ConvolveOp op = new ConvolveOp(kernel);
		op.filter(bi, buff);
		
		return buff;
	}

Java - Color2Gray

public BufferedImage toGray(BufferedImage bi)
	{
		ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
		ColorConvertOp op = new ColorConvertOp(cs, null);
		
		return op.filter(bi, null);
	}

Java - rotate -90°/90°

public BufferedImage rotate90DX(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB(height-1-j, width-1-i, bi.getRGB(i, j));
		
		return biFlip;
	}


	public BufferedImage rotate90SX(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(height, width, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB(j, i, bi.getRGB(i, j));
		
		return biFlip;
	}

Java - HalfFlipH / HalfFlipW

	public BufferedImage halfFlipW(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(width, height, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height/2; j++)
			{
				biFlip.setRGB(i, j, bi.getRGB(i, j));
				biFlip.setRGB(i, (height-1)-j, bi.getRGB(i, j));
			}
		
		return biFlip;
	}


	public BufferedImage halfFlipH(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(width, height, bi.getType());
		
		for(int i=0; i<width/2; i++)
			for(int j=0; j<height; j++)
			{
				biFlip.setRGB(i, j, bi.getRGB(i, j));
				biFlip.setRGB((width-1)-i, j, bi.getRGB(i, j));
			}
		
		return biFlip;
	}

Java - FlipH / FlipW

public BufferedImage flipH(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(width, height, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB((width-1)-i, j, bi.getRGB(i, j));
		
		return biFlip;
	}


	
	public BufferedImage flipW(BufferedImage bi)
	{
		int width = bi.getWidth();
		int height = bi.getHeight();
		
		BufferedImage biFlip = new BufferedImage(width, height, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biFlip.setRGB(i, (height-1)-j, bi.getRGB(i, j));
		
		return biFlip;
	}

Java - ZoomOUT image

public BufferedImage zoomOut(BufferedImage bi, int scale)
	{
		int width = bi.getWidth() / scale;
		int height = bi.getHeight() / scale;
		
		BufferedImage biScale = new BufferedImage(width, height, bi.getType());
		
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biScale.setRGB(i, j, bi.getRGB(i*scale, j*scale));
		
		return biScale;
	}

Java - ZoomIN Image

public BufferedImage zoomIn(BufferedImage bi, int scale)
	{
		int width = scale * bi.getWidth();
		int height = scale * bi.getHeight();
		
		BufferedImage biScale = new BufferedImage(width, height, bi.getType());
		
                // Cicla dando un valore medio al pixel corrispondente
		for(int i=0; i<width; i++)
			for(int j=0; j<height; j++)
				biScale.setRGB(i, j, bi.getRGB(i/scale, j/scale));
		
		return biScale;
	}

J2ME - System Properties

// System Properties J2ME

package System;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SystemProperties extends MIDlet implements CommandListener
{
	private Command esci;
	
	private Display display;
	
	private Form form;
	
	protected void startApp() throws MIDletStateChangeException
	{
		display = Display.getDisplay(this);
		
		form = new Form("System Propiertis");
		form.setCommandListener(this);
		
		esci = new Command("Esci", Command.EXIT, 0);
		form.addCommand(esci);
		
		Runtime rt = Runtime.getRuntime();
		rt.gc(); // Garbage Collection
		
		form.append("Free Memory: " + rt.freeMemory() + "\n");
		form.append("Total Memory: " + rt.totalMemory() + "\n");
		form.append(showProp("microedition.configuration"));
		form.append(showProp("microedition.platform"));
		form.append(showProp("microedition.locale"));
		form.append(showProp("microedition.encoding"));
		form.append(showProp("microedition.encodingClass"));
		form.append(showProp("microedition.http_proxy"));
		
		display.setCurrent(form);
	}

	protected void pauseApp()
	{

	}

	protected void destroyApp(boolean unconditional) throws MIDletStateChangeException
	{
		notifyDestroyed();
	}

	public String showProp(String str)
	{
		String value = System.getProperty(str);
		StringBuffer stringbuffer = new StringBuffer(50);
		
		stringbuffer.setLength(0);
		stringbuffer.append(str);
		stringbuffer.append(" = ");
		
		if(value == null)
			stringbuffer.append("<undefined>");
		else
		{
			stringbuffer.append("\"");
			stringbuffer.append(value);
			stringbuffer.append("\"");
		}
		
		stringbuffer.append("\n");
		
		return stringbuffer.toString();
	}
	
	public void commandAction(Command c, Displayable d)
	{
		if(c == esci)
		{
			try
			{
				destroyApp(true);
			}
			catch(MIDletStateChangeException e)
			{
				showException(e);
			}
		}
	}
	
	public void showException(Exception e)
	{
		Alert alert = new Alert("Errore !!!");
		alert.setString(e.getMessage());
		alert.setType(AlertType.ERROR);
		alert.setTimeout(Alert.FOREVER);
		
		display.setCurrent(alert);
	}
}
« Newer Snippets
Older Snippets »
Showing 11-20 of 21 total