Friday, April 27, 2012
Latitude and Longitude in Blackberry
handleGPS handleGPS=new handleGPS();
int m_bbHandle = CodeModuleManager.getModuleHandle("net_rim_bb_lbs");
if(m_bbHandle>0){
Dialog.alert("GPS not found");
}
else{
Dialog.alert("GPS found");
}
public class handleGPS{
static GPSThread gpsThread;
public static double latitude ;
public static double longitude;
public handleGPS(){
gpsThread = new GPSThread();
gpsThread.start();
}
private static class GPSThread extends Thread{
public void run() {
Criteria myCriteria = new Criteria();
myCriteria.setCostAllowed(false);
try {
LocationProvider myLocationProvider = LocationProvider.getInstance(myCriteria);
try {
Location myLocation = myLocationProvider.getLocation(300);
latitude = myLocation.getQualifiedCoordinates().getLatitude();
longitude = myLocation.getQualifiedCoordinates().getLongitude();
System.out.print("latitude= "+latitude+" longitude="+longitude);
}
catch ( InterruptedException iex ) {
return;
}
catch ( LocationException lex ) {
return;
}
}catch ( LocationException lex ) {
return;
}
return;
}
}
}
SHA1 Encryption in Blackberry
public class Sha1 {
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while (two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text) {
SHA1Digest sha1Digest = new SHA1Digest();
sha1Digest.update(text.getBytes(), 0, text.length());
byte[] hashValBytes = new byte[sha1Digest.getDigestLength()];
hashValBytes = sha1Digest.getDigest();
return convertToHex(hashValBytes);
}
}
PictureScroll in Blackberry
private PictureScrollField _pictureScrollField;
ScrollEntry[] entries = new ScrollEntry[ 4 ];
entries[0] = new ScrollEntry(Bitmap.getBitmapResource("image1.jpg"), "BlackBerry", "image 1");
entries[1] = new ScrollEntry(Bitmap.getBitmapResource("image2.jpg"), "Blue logo", "image 2");
entries[2] = new ScrollEntry(Bitmap.getBitmapResource("image3.jpg"), "Black logo", "image 3");
entries[3] = new ScrollEntry(Bitmap.getBitmapResource("image3.jpg"), "Building", "image 4");
_pictureScrollField = new PictureScrollField(150, 100);
_pictureScrollField.setData(entries, 0);
add(_pictureScrollField);
Image Zoom In and Zoom Out in Blackberry
The following code will give you the functionality like- Zoom in and out.
public final class ZoomScreenDemo extends UiApplication
{
public static void main(final String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
UiApplication app = new ZoomScreenDemo();
app.enterEventDispatcher();
}
/**
* Creates a new ZoomScreenDemo object
*/
public ZoomScreenDemo()
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Click trackball or screen to zoom");
}
});
pushScreen(new ZoomScreenDemoScreen());
}
public final static class ZoomScreenDemoScreen extends MainScreen
{
private EncodedImage _image;
/**
* Creates a new ZoomScreenDemoScreen object
*/
public ZoomScreenDemoScreen()
{
setTitle("Zoom Screen Demo");
_image = EncodedImage.getEncodedImageResource("img/building.jpg");
BitmapField bitmapField = new BitmapField(_image.getBitmap(), FIELD_HCENTER | FOCUSABLE);
add(bitmapField);
}
/**
* @see Screen#navigationClick(int, int)
*/
protected boolean navigationClick(int status, int time)
{
// Push a new ZoomScreen if track ball or screen is clicked
UiApplication.getUiApplication().pushScreen(new ZoomScreen(_image));
return true;
}
/**
* @see Screen#touchEvent(TouchEvent)
*/
protected boolean touchEvent(TouchEvent message)
{
if(message.getEvent() == TouchEvent.CLICK)
{
UiApplication.getUiApplication().pushScreen(new ZoomScreen(_image));
}
return super.touchEvent(message);
}
}
}
public final class ZoomScreenDemo extends UiApplication
{
public static void main(final String[] args)
{
// Create a new instance of the application and make the currently
// running thread the application's event dispatch thread.
UiApplication app = new ZoomScreenDemo();
app.enterEventDispatcher();
}
/**
* Creates a new ZoomScreenDemo object
*/
public ZoomScreenDemo()
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Click trackball or screen to zoom");
}
});
pushScreen(new ZoomScreenDemoScreen());
}
public final static class ZoomScreenDemoScreen extends MainScreen
{
private EncodedImage _image;
/**
* Creates a new ZoomScreenDemoScreen object
*/
public ZoomScreenDemoScreen()
{
setTitle("Zoom Screen Demo");
_image = EncodedImage.getEncodedImageResource("img/building.jpg");
BitmapField bitmapField = new BitmapField(_image.getBitmap(), FIELD_HCENTER | FOCUSABLE);
add(bitmapField);
}
/**
* @see Screen#navigationClick(int, int)
*/
protected boolean navigationClick(int status, int time)
{
// Push a new ZoomScreen if track ball or screen is clicked
UiApplication.getUiApplication().pushScreen(new ZoomScreen(_image));
return true;
}
/**
* @see Screen#touchEvent(TouchEvent)
*/
protected boolean touchEvent(TouchEvent message)
{
if(message.getEvent() == TouchEvent.CLICK)
{
UiApplication.getUiApplication().pushScreen(new ZoomScreen(_image));
}
return super.touchEvent(message);
}
}
}
Get Screen Resolution in Blackberry
The following code will give you the Screen Resolution of your blackberry.
Display.getWidth();
Display.getHeight();
Display.getWidth();
Display.getHeight();
Display Image From URL in Blackberry
This code will give you the image from the url.
URLBitmapField post_img= new URLBitmapField(image_url);
add(post_img);
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
public class URLBitmapField extends BitmapField implements URLDataCallback {
EncodedImage result = null;
public static EncodedImage _encoded_img = null;
int _imgWidth = 52;
int _imgHeight = 62;
int _imgMargin = 10;
public URLBitmapField(String url) {
try {
http_image_data_extrator.getWebData(url, this);
}
catch (Exception e) {}
}
public Bitmap getBitmap() {
if (_encoded_img == null) return null;
return _encoded_img.getBitmap();
}
public void callback(final String data) {
if (data.startsWith("Exception")) return;
try {
byte[] dataArray = data.getBytes();
_encoded_img = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); // with scale
_encoded_img = sizeImage(_encoded_img, _imgWidth, _imgHeight);
setImage(_encoded_img);
UiApplication.getUiApplication().getActiveScreen().invalidate();
}
catch (final Exception e){}
}
public EncodedImage sizeImage(EncodedImage image, int width, int height) {
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,requiredHeightFixed32);
result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return result;
}
}
public interface URLDataCallback {
public void callback(String data);
}
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;
public class http_image_data_extrator {
static String url_="";
static StringBuffer rawResponse=null;
public static void getWebData(String url, final URLDataCallback callback) throws IOException {
HttpConnection connection = null;
InputStream inputStream = null;
try {
if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)&& RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
url += ";interface=wifi";
}
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
String location=connection.getHeaderField("location");
if(location!=null){
if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)&& RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
location += ";interface=wifi";
}
connection = (HttpConnection) Connector.open(location, Connector.READ, true);
}else{
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
}
inputStream = connection.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK){
throw new IOException("HTTP response code: "+ responseCode);
}
final String result = rawResponse.toString();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run(){
callback.callback(result);
}
});
}
catch (final Exception ex) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());
}
});
}
}
}
URLBitmapField post_img= new URLBitmapField(image_url);
add(post_img);
import net.rim.device.api.math.Fixed32;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
public class URLBitmapField extends BitmapField implements URLDataCallback {
EncodedImage result = null;
public static EncodedImage _encoded_img = null;
int _imgWidth = 52;
int _imgHeight = 62;
int _imgMargin = 10;
public URLBitmapField(String url) {
try {
http_image_data_extrator.getWebData(url, this);
}
catch (Exception e) {}
}
public Bitmap getBitmap() {
if (_encoded_img == null) return null;
return _encoded_img.getBitmap();
}
public void callback(final String data) {
if (data.startsWith("Exception")) return;
try {
byte[] dataArray = data.getBytes();
_encoded_img = EncodedImage.createEncodedImage(dataArray, 0, dataArray.length); // with scale
_encoded_img = sizeImage(_encoded_img, _imgWidth, _imgHeight);
setImage(_encoded_img);
UiApplication.getUiApplication().getActiveScreen().invalidate();
}
catch (final Exception e){}
}
public EncodedImage sizeImage(EncodedImage image, int width, int height) {
int currentWidthFixed32 = Fixed32.toFP(image.getWidth());
int currentHeightFixed32 = Fixed32.toFP(image.getHeight());
int requiredWidthFixed32 = Fixed32.toFP(width);
int requiredHeightFixed32 = Fixed32.toFP(height);
int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
requiredWidthFixed32);
int scaleYFixed32 = Fixed32.div(currentHeightFixed32,requiredHeightFixed32);
result = image.scaleImage32(scaleXFixed32, scaleYFixed32);
return result;
}
}
public interface URLDataCallback {
public void callback(String data);
}
import java.io.IOException;
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import net.rim.device.api.system.RadioInfo;
import net.rim.device.api.system.WLANInfo;
import net.rim.device.api.ui.UiApplication;
public class http_image_data_extrator {
static String url_="";
static StringBuffer rawResponse=null;
public static void getWebData(String url, final URLDataCallback callback) throws IOException {
HttpConnection connection = null;
InputStream inputStream = null;
try {
if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)&& RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
url += ";interface=wifi";
}
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
String location=connection.getHeaderField("location");
if(location!=null){
if ((WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED)&& RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN)) {
location += ";interface=wifi";
}
connection = (HttpConnection) Connector.open(location, Connector.READ, true);
}else{
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
}
inputStream = connection.openInputStream();
byte[] responseData = new byte[10000];
int length = 0;
rawResponse = new StringBuffer();
while (-1 != (length = inputStream.read(responseData))) {
rawResponse.append(new String(responseData, 0, length));
}
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK){
throw new IOException("HTTP response code: "+ responseCode);
}
final String result = rawResponse.toString();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run(){
callback.callback(result);
}
});
}
catch (final Exception ex) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
callback.callback("Exception (" + ex.getClass() + "): " + ex.getMessage());
}
});
}
}
}
Creating Popup Screen in Blackberry
UiApplication.getUiApplication().pushScreen(new MyPopup());
public class MyPopup extends PopupScreen{
public MyPopup() {
super(new VerticalFieldManager(), Field.FOCUSABLE);
add();//add Fields (checkbox , radio buttons.......) here.
}
Friday, April 20, 2012
Record and Save Audio File in SD Card in Blackberry
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import java.lang.*;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.media.*;
import javax.microedition.media.Manager;
import java.io.*;
import javax.microedition.media.control.*;
public class AudioRecording extends UiApplication
{
public static void main(String[] args)
{
AudioRecording app = new AudioRecording();
app.enterEventDispatcher();
}
public AudioRecording()
{
pushScreen(new AudioRecordingDemoScreen());
}
private class AudioRecordingDemoScreen extends MainScreen
{
private AudioRecorderThread _recorderThread;
ByteArrayOutputStream bt;
DataOutputStream ot;
public AudioRecordingDemoScreen()
{
//setTitle("Audio recording demo");
addMenuItem(new StartRecording());
addMenuItem(new StopRecording());
}
private class StartRecording extends MenuItem
{
public StartRecording()
{
super("Start recording", 0, 100);
}
public void run()
{
try
{
AudioRecorderThread newRecorderThread = new AudioRecorderThread();
newRecorderThread.start();
_recorderThread = newRecorderThread;
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
private class StopRecording extends MenuItem
{
public StopRecording()
{
super("Stop recording", 0, 100);
}
public void run()
{
try
{
if (_recorderThread != null)
{
_recorderThread.stop();
}
}
catch (Exception e)
{
Dialog.alert(e.toString());
}
}
}
private class AudioRecorderThread extends Thread implements javax.microedition.media.PlayerListener
{
private Player _player;
private RecordControl _rcontrol;
private ByteArrayOutputStream _output;
private byte _data[];
AudioRecorderThread() {}
private int getSize()
{
return (_output != null ? _output.size() : 0);
}
private byte[] getVoiceNote()
{
return _data;
}
public void run() {
try {
// Create a Player that captures live audio.
_player = Manager.createPlayer("capture://audio");
_player.realize();
// Get the RecordControl, set the record stream,
_rcontrol = (RecordControl)_player.getControl("RecordControl");
//Create a ByteArrayOutputStream to capture the audio stream.
_output = new ByteArrayOutputStream();
_rcontrol.setRecordStream(_output);
_rcontrol.startRecord();
_player.start();
} catch (final Exception e) {
UiApplication.getUiApplication().invokeAndWait(new Runnable() {
public void run() {
Dialog.inform(e.toString());
}
});
}
}
public void stop() {
try {
//Stop recording, capture data from the OutputStream,
//close the OutputStream and player.
_rcontrol.commit();
_data = _output.toByteArray();
_output.close();
_player.close();
saveRecordedFile(_data);
} catch (Exception e) {
synchronized (UiApplication.getEventLock()) {
Dialog.inform(e.toString());
}
}
}
public void playerUpdate(Player player, String event, Object eventData)
{
Dialog.alert("Player " + player.hashCode() + " got event " + event + ": " + eventData);
}
}
}
public static boolean saveRecordedFile(byte[] data) {
try {
String filePath1 = System.getProperty("fileconn.dir.music");
String fileName = "sample";
boolean existed = true;
for (int i = 0; i < Integer.MAX_VALUE; i++) {
try {
FileConnection fc = (FileConnection) Connector.open(filePath1 + fileName+ ".mp3");
if (!fc.exists()) {
existed = false;
}
fc.close();
} catch (IOException e) {
Dialog.alert("unable to save");
return existed;
}
if (!existed) {
fileName +=".mp3";
filePath1 += fileName;
break;
}
}
System.out.println(filePath1);
System.out.println("");
FileConnection fconn = (FileConnection) javax.microedition.io.Connector .open(filePath1, javax.microedition.io.Connector.READ_WRITE);
if (fconn.exists())
fconn.delete();
fconn.create();
OutputStream outputStream = fconn.openOutputStream();
outputStream.write(data);
outputStream.close();
fconn.close();
return true;
} catch (Exception e) {
}
return false;
}
}
Read Image from SD Card in Blackberry
public
Bitmap getImage(){
Bitmap bitmapImage=
null
;
try
{
InputStream input;
FileConnection fconn = (FileConnection) Connector.open(
"file:///store/home/user/dirname/imgname.png"
, Connector.READ_WRITE);
input = fconn.openInputStream();
ByteArrayOutputStream baos =
new
ByteArrayOutputStream();
int
j =
0
;
while
((j=input.read()) != -
1
) {
baos.write(j);
}
byte
[] byteArray = baos.toByteArray();
bitmapImage = Bitmap.createBitmapFromBytes(byteArray,
0
,byteArray.length,
1
);
}
catch
(Exception ioe){
System.out.println(ioe);
}
return
bitmapImage;
}
Subscribe to:
Posts (Atom)