Tampilkan postingan dengan label Java. Tampilkan semua postingan
Tampilkan postingan dengan label Java. Tampilkan semua postingan

Erase Some Characters from Each Line of a Text File - Java Program

Posted by Unknown Minggu, 11 Agustus 2013 0 komentar
Following Java program erase user given fixed number of characters from each line of a text file. Here the number of characters to be erased is 3. For the convenience input and output files are given as two separate file.  Output file may contain the result you want.

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;

public class CharEraser {

public static void main(String[] args) throws IOException {

String inputfile = "input.txt";
String outputfile = "output.txt";
int number_of_char_to_erased = 3;

String line = "";

File input = new File(inputfile);
Scanner scan = new Scanner(input);
File output = new File(outputfile);
PrintStream print = new PrintStream(output);

while (scan.hasNext()) {
line = scan.nextLine();
line = line.substring(number_of_char_to_erased);
print.println(line);
}

scan.close();
print.close();
}
}




Baca Selengkapnya ....

Brightness Changing Algorithm - Image Processing Basic

Posted by Unknown Selasa, 22 Mei 2012 0 komentar
There are many libraries which are used to Image processing applications. But the algorithms behind these libraries use basic mathematical operation. I hope to bring you series of blog posts which describe the fundamental image processing algorithms implemented in mathematical operation without using any library. Following java class change the brightness of an image.
We should input our image to the method changeBrightness as a BufferedImage. increasingFactor is an integer which determine the brightness level of the image. Positive values will increase the brightness while negative values decrees. Range of the increasingFactor should be between -255 to 255.

Original Image
Brightness Increased
Brightness Decreased

package lakj.comspace.imageprocessor;

import java.awt.Color;
import java.awt.image.BufferedImage;

/**
*Brightness Changing Class
*by
*Lak J Comspace
*
*/
public class Brightness {

public BufferedImage changeBrightness(BufferedImage inImage, int increasingFactor) {

//size of input image
int w = inImage.getWidth();
int h = inImage.getHeight();

BufferedImage outImage = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

//Pixel by pixel navigation loop
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {


//get the RGB component of input imge pixel
Color color = new Color(inImage.getRGB(i, j));

int r, g, b;

//change the value of each component
r = color.getRed() + increasingFactor;
g = color.getGreen() + increasingFactor;
b = color.getBlue() + increasingFactor;


//r,g,b values which are out of the range 0 to 255 should set to 0 or 255
if (r >= 256) {
r = 255;
} else if (r < 0) {
r = 0;
}

if (g >= 256) {
g = 255;
} else if (g < 0) {
g = 0;
}

if (b >= 256) {
b = 255;
} else if (b < 0) {
b = 0;
}

//set output image pixel component
outImage.setRGB(i, j, new Color(r, g, b).getRGB());

}
}

return outImage;
}
}

Baca Selengkapnya ....

Android Application to Send a File to Remote Server

Posted by Unknown Jumat, 30 Maret 2012 0 komentar
This simple Android application sends a file to a remote server. In this example code server runs on local host. When the Send button is clicked the file is sent to the server.

Android Client Application
SimpleClientActivity.java
/*
* This is a simple Android mobile client
* This application send any file to a remort server when the
* send button is pressed
* Author by Lak J Comspace
*/

package lakj.comspace.simpleclient;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SimpleClientActivity extends Activity {

private Socket client;
private FileInputStream fileInputStream;
private BufferedInputStream bufferedInputStream;
private OutputStream outputStream;
private Button button;
private TextView text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button1); //reference to the send button
text = (TextView) findViewById(R.id.textView1); //reference to the text view

//Button press event listener
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {


File file = new File("/mnt/sdcard/input.jpg"); //create file instance

try {

client = new Socket("10.0.2.2", 4444);

byte[] mybytearray = new byte[(int) file.length()]; //create a byte array to file

fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);

bufferedInputStream.read(mybytearray, 0, mybytearray.length); //read the file

outputStream = client.getOutputStream();

outputStream.write(mybytearray, 0, mybytearray.length); //write file to the output stream byte by byte
outputStream.flush();
bufferedInputStream.close();
outputStream.close();
client.close();

text.setText("File Sent");


} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}
});

}
}


main.xml






AndroidManifest.xml


















Server Application
main.java
/*
* This is a simple server application
* This server receive a file from a Android client and save it in a given place.
* Author by Lak J Comspace
*/
package simpleserver;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStream inputStream;
private static FileOutputStream fileOutputStream;
private static BufferedOutputStream bufferedOutputStream;
private static int filesize = 10000000; // filesize temporary hardcoded
private static int bytesRead;
private static int current = 0;

public static void main(String[] args) throws IOException {


serverSocket = new ServerSocket(4444); //Server socket

System.out.println("Server started. Listening to the port 4444");


clientSocket = serverSocket.accept();


byte[] mybytearray = new byte[filesize]; //create byte array to buffer the file

inputStream = clientSocket.getInputStream();
fileOutputStream = new FileOutputStream("D:\\output.jpg");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

System.out.println("Receiving...");

//following lines read the input slide file byte by byte
bytesRead = inputStream.read(mybytearray, 0, mybytearray.length);
current = bytesRead;

do {
bytesRead = inputStream.read(mybytearray, current, (mybytearray.length - current));
if (bytesRead >= 0) {
current += bytesRead;
}
} while (bytesRead > -1);


bufferedOutputStream.write(mybytearray, 0, current);
bufferedOutputStream.flush();
bufferedOutputStream.close();
inputStream.close();
clientSocket.close();
serverSocket.close();

System.out.println("Sever recieved the file");

}
}


Baca Selengkapnya ....

Simple Client-Server Application for Android

Posted by Unknown Kamis, 29 Maret 2012 0 komentar
This application is a simple client-server application which has a Android mobile client and a Java server which is run on a machine. In this example, client is run on the Android emulator and the server is run on the local host. In Android 10.0.2.2 is the IP address for local host. This application allow to type a text message on a text field and when the Send button is press the message is sent to the server. Server continuously listen to the port. When there is a incoming message server read it and show it on the standard output.



Client Side Application


SlimpleClientActivity.java

/*
* This is a simple Android mobile client
* This application read any string messege typed on the text field and
* send it to the server when the Send button is pressed
* Author by Lak J Comspace
*/

package lakj.comspace.simpleclient;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SimpleClientActivity extends Activity {

private Socket client;
private PrintWriter printwriter;
private EditText textField;
private Button button;
private String messsage;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

textField = (EditText) findViewById(R.id.editText1); //reference to the text field
button = (Button) findViewById(R.id.button1); //reference to the send button

//Button press event listener
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

messsage = textField.getText().toString(); //get the text message on the text field
textField.setText(""); //Reset the text field to blank

try {

client = new Socket("10.0.2.2", 4444); //connect to server
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage); //write the message to output stream

printwriter.flush();
printwriter.close();
client.close(); //closing the connection

} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

}
}


main.xml












AndroidManifest.xml



















Server Side Application


main.java

/*
* This is a simple server application
* This server receive a string message from the Android mobile phone
* and show it on the console.
* Author by Lak J Comspace
*/
package simpleserver;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;

public static void main(String[] args) {

try {
serverSocket = new ServerSocket(4444); //Server socket

} catch (IOException e) {
System.out.println("Could not listen on port: 4444");
}

System.out.println("Server started. Listening to the port 4444");

while (true) {
try {

clientSocket = serverSocket.accept(); //accept the client connection
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader); //get the client message
message = bufferedReader.readLine();

System.out.println(message);
inputStreamReader.close();
clientSocket.close();

} catch (IOException ex) {
System.out.println("Problem in message reading");
}
}

}
}

Baca Selengkapnya ....

Roman Number Converter - Java Program

Posted by Unknown Jumat, 10 Februari 2012 0 komentar

This is a simple Java program to convert Roman numbers to Hindu-Arabic numbers. This Java method "convertRomToInt()" gets the Roman number as a string and converts it in to corresponding Integer value.
Even though this program seems simple, the algorithm is bit complex. When we convert a Roman number to Hindu-Arabic number manually, we do it in the following manner.
MCMXLIV = 1000 + (1000 − 100) + (50 − 10) + (5 − 1) = 1944
Value of each number is added together. When smaller values precede larger values, the smaller values are subtracted from the larger values, and the result is added to the total.
This algorithm use above technique to the conversion. "for" loop gets each character of the roman number string from the beginning and gets its integer value from the "getInt()"  method. At each iteration character is compared with the next character value. If the next character is the same, the value is added to mediator to add in the next iteration. If it is large, the value is added to mediator to subtract in the next iteration.

//Converting Roman number to Arabic number
public static int convertRomToInt(String romanNum) {

int result = 0;
int mediator = 0;

int length = romanNum.length(); //get number string length


for (int i = 0; i < length - 1; i++) { //this loop will add each Roman character value

if (getInt(romanNum.charAt(i)) > getInt(romanNum.charAt(i + 1))) {

result = result + getInt(romanNum.charAt(i)) + mediator;
mediator = 0;

} else if (getInt(romanNum.charAt(i)) == getInt(romanNum.charAt(i + 1))) {

mediator = mediator + getInt(romanNum.charAt(i));

} else if (getInt(romanNum.charAt(i)) < getInt(romanNum.charAt(i + 1))) {

mediator = -mediator - getInt(romanNum.charAt(i));

}

}

result = result + mediator + getInt(romanNum.charAt(length - 1));

return result;

}

//Get integer value for corresponding Roman character
public static int getInt(char romanChar) {

if (romanChar == 'I')
return 1;
else if (romanChar == 'V') {
return 5;
} else if (romanChar == 'X') {
return 10;
} else if (romanChar == 'L') {
return 50;
} else if (romanChar == 'C') {
return 100;
} else if (romanChar == 'D') {
return 500;
} else if (romanChar == 'M') {
return 1000;
}
return (Integer) null;
}



Baca Selengkapnya ....

Porting J2ME Applications to Android

Posted by Unknown Jumat, 20 Februari 2009 0 komentar
When the new Google Android platform was introduced, there were initially a small number applications available, so Google spent $10 million to attract developers to their Android Developer Challenge before the T-Mobile G1 phone was released.  The existing Java ME community quickly realized that there was a huge opportunity if there can be tools developed that allowed the existing Java ME applications to be run on the Android platform.  

There are 2 tools that may be somewhat useful for allowing Java ME applications to run on Android:
  1. MicroEmulator is a pure Java implementation of Java ME in J2SE.  The advantage is that it is an open source project and is licensed under LPGL so that it is possible to distribute commercial software with its libraries.  See MicroEmu: Running Java ME applications on Android for further details.  
  2. Netmite has developed an application, J2ME MIDP Runner to allow running Java ME applications on Android without significant source code changes that is readily available to download from the Android Market
MicroEmulator supports the following features :  
  • MIDP 2.0 (TextField UI - not yet implemented)
  • Skinnable and configurable interface
  • Generic Connection Framework
  • JSR 135 (Mobile Multimedia API); JSR 75 (File Connection API)
  • JSR-179 (Location APIs) targeted to be available as a commercial extension
  • JSR-184 (3D graphics) on top of Android OpenGL ES targeted to be available in the future as a commercial extension
Bookmark and Share


Baca Selengkapnya ....
Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android photography.