×
Namespaces

Variants
Actions

How to create a magnifying lens application in Asha platform

From Nokia Developer Wiki
Jump to: navigation, search
{{{width}}}
09 Feb
2014

This article explains how to create a magnifying lens application for the Asha platform.

Note.pngNote: This article was an entry in the Nokia Asha Wiki Competition 2013H2.

Article Metadata
Code ExampleCompatibilityArticle
Created: jsivaguru (15 Dec 2013)
Last edited: jsivaguru (10 Feb 2014)

Introduction

We will use the Asha platform's excellent image processing capabilities to make a magnifying glass application.

We will create an application that magnifies a portion of a still image by applying cropping and scaling techniques. Note that the platform does not permit magnification of a live feed from the camera.

Magnification application

Coding

The core of the magnification logic is in the following code.

This is the pointer pressed method of an image added to a Form.

Whenever the user clicks on a particular location in an image, we create a fixed size rectangle that includes the selected pixel. We use the transform control in the Advanced Multimedia Supplements (AMMS) APIs to crop the image. That gives us a rectangular area that surrounds the selected pixel.

We then use the AMMS APIs again to scale the rectangular region to the required magnification level.

               int sub_value_x = this.getAbsoluteX() + this.getStyle().getPadding(Component.LEFT);
int sub_value_y = this.getAbsoluteY() + this.getStyle().getPadding(Component.TOP);
 
// We are choosing a point that is diagonally across from the pointer pressed location.
 
sourceX = ((2 * (x) - length_rect) / 2) - sub_value_x;
sourceY = ((2 * (y) - breadth_rect) / 2) - sub_value_y;
 
int targetWidth = breadth_rect;
int targetHeight = length_rect;
 
//Transforming to required size
transformControl.setSourceRect(sourceX, sourceY, breadth_rect, length_rect);
transformControl.setTargetSize(targetWidth, targetHeight, 0);
 
// Setting input
try
{
mp.setInput(m.getFinal_image());
} catch (MediaException ex)
{
ex.printStackTrace();
}
//Setting output
ByteArrayOutputStream bos = new ByteArrayOutputStream();
mp.setOutput(bos);
 
// Performing the scale for the first time
transformControl.setEnabled(true);
try
{
mp.complete();
} catch (MediaException ex)
{
ex.printStackTrace();
}
byte[] b = bos.toByteArray();
 
// Higher levels of scale for zoom factor greater than one.
if (zoomfactor > 1)
{
try
{
mp.setInput(javax.microedition.lcdui.Image.createImage(b, 0, b.length));
bos = new ByteArrayOutputStream();
mp.setOutput(bos);
transformControl.setSourceRect(0, 0, length_rect, breadth_rect);
 
// The target width and height is in multiples of the zoom factor.
int targetW = (int) (zoomfactor * breadth_rect);
int targetH = (int) (zoomfactor * length_rect);
 
transformControl.setTargetSize(targetW, targetH, 0);
transformControl.setEnabled(true);
mp.complete();
b = bos.toByteArray();
 
} catch (MediaException ex)
{
ex.printStackTrace();
}
 
}
 
// Creating the scaled(Magnified) image.
magnified_image = Image.createImage(b, 0, b.length);
top.removeAll();
top.addComponent(new Label(magnified_image));
top.repaint();

Summary

This article demonstrates how to use the Asha Platform's camera as a magnifying lens.

This page was last modified on 10 February 2014, at 16:00.
180 page views in the last 30 days.