List of all Iphone and Blackberry Development codes in a one click Iphone,objective C,xcode,blackberry Development,iOS Development

Tuesday, May 15, 2012

Draw painting and save as Image in Blackberry




Draw your paintings and save it as an image. This will help you for doing that.

















Background white image for drawing your paintings.

_bitmap= Bitmap.getBitmapResource("background.png");//white color background image
_signatureField = new SignatureField(_bitmap);
add(_signatureField);


When you press the capture button on the menu, it will save your painting as an image.

private MenuItem _captureItem = new MenuItem("Capture", 200000, 10) {
public void run() {
//Create a new instance of the encoder to encode the bitmap into an image
PNGEncoder encoder = new PNGEncoder(_bitmap, true);
try {
byte[] imageBytes = encoder.encode(true);
EncodedImage image = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
try {
FileConnection fconn = (FileConnection) Connector.open(
new StringBuffer().append("file:///store/home/user/").append("Signature-").append(System.currentTimeMillis()).append(".png").toString());
if (!fconn.exists())
fconn.create();
DataOutputStream ds = fconn.openDataOutputStream();
ds.write(image.getData());
ds.close();
fconn.close();
} catch (IOException e) {
Dialog.alert(e.getMessage());
e.printStackTrace();
}
} catch (IOException e) {
Dialog.alert(e.getMessage());
e.printStackTrace();
}
}
};

/**
* MenuItem to clear the signature field
*/
private MenuItem _clearItem = new MenuItem("Clear", 200000, 10) {
public void run() {
_signatureField.clear();
}

};

/**
* MenuItem to close the application
*/
private MenuItem _closeItem = new MenuItem("Close", 200000, 10) {
public void run() {
onClose();
}
};

/**
* Create the menu
*/
protected void makeMenu(Menu menu, int instance) {
menu.add(_captureItem);
menu.add(_clearItem);
menu.add(_closeItem);
}

final class SignatureField extends BitmapField {

private Graphics _graphics;

/**
* Constructor
*/
public SignatureField(Bitmap b) {
this.setBitmap(b);
_graphics = new Graphics(b);
}

/**
* Handle touch events
*/
protected boolean touchEvent(TouchEvent message) {
try {
if (message.getEvent() == TouchEvent.MOVE) { //Move event fired
//Get the move points
int pointsSize = message.getMovePointsSize();

if (pointsSize > 1)
{
int[] xPoints = new int[pointsSize];
int[] yPoints = new int[pointsSize];
message.getMovePoints(1, xPoints, yPoints, null);
drawPath(xPoints,yPoints);
}
} else if (message.getEvent() == TouchEvent.GESTURE) { //Gesture event fired
TouchGesture gesture = message.getGesture();
if (gesture.getEvent() == TouchGesture.TAP) { //Tap Gesture
//Since we have a tap only draw a single point
int xPoint = message.getX(1);
int yPoint = message.getY(1);
drawPoint(xPoint,yPoint);
} else if (gesture.getEvent() == TouchGesture.SWIPE) { //Swipe Gesture
//Get the move points
int pointsSize = message.getMovePointsSize();
int[] xPoints = new int[pointsSize];
int[] yPoints = new int[pointsSize];
message.getMovePoints(1, xPoints, yPoints, null);
drawPath(xPoints,yPoints);
}
}
} catch (Throwable e) {
throw new RuntimeException(e.toString());
}
return true;
}

/**
* Draw a path through the set of points
*/
private void drawPath(int[] xPoints, int[] yPoints) {
int oldColor = _graphics.getColor();
//Draw a path through the points
_graphics.setColor(Color.RED);
_graphics.drawPathOutline(xPoints,yPoints, null, null, false);
_graphics.setColor(oldColor);
//Repaint
invalidate();
}

/**
* Draw a point
*/
private void drawPoint(int xPoint, int yPoint) {
int oldColor = _graphics.getColor();
_graphics.setColor(0x000000);
_graphics.drawPoint(xPoint, yPoint);
_graphics.setColor(oldColor);
//Repaint
invalidate();
}

/**
* Clear the field
*/
private void clear() {
int oldColor = _graphics.getColor();
_graphics.setColor(0xFFFFFF);
_graphics.fillRect(0, 0, _bitmap.getWidth(), _bitmap.getHeight());
_graphics.setColor(oldColor);
invalidate();
}
}




        

No comments:

Post a Comment