Mirroring the image in blackberry - blackberry

Hi I want to convert normal image into mirror i.e flop effect in BLACKBERRY app.I have tried this code but unable too convert...Is there anyone to help me to do this...
If you have a different logic to do this please share..
public Bitmap changetoFlopEffect(Bitmap bitmap){
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
int[] newargb =new int[bitmap.getWidth() * bitmap.getHeight()];
int length=bitmap.getWidth();
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for(int i=0;i<=bitmap.getHeight();i++)
{
for(int j=bitmap.getWidth(),k=0;j>0;j--)
{
//newargb[k]=argb[j];
int swap=argb[j];
newargb[k]=swap;
k++;
}
}
bitmap.setARGB(newargb,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
return bitmap;
}

Your corner conditions seem wrong and the pixels you swipe are from the first row only. Try this:
public Bitmap changetoFlopEffect(Bitmap bitmap){
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
int[] newargb =new int[bitmap.getWidth() * bitmap.getHeight()];
int length=bitmap.getWidth();
int newIndex = 0;
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < bitmap.getHeight(); i++)
for (int j = bitmap.getWidth()-1; j >= 0 ; j--){
newargb[newIndex] = argb[i * bitmap.getWidth() + j];
newIndex++;
}
bitmap.setARGB(newargb,0,bitmap.getWidth(),0,0,bitmap.getWidth(),bitmap.getHeight());
return bitmap;
}

Related

How do I convert ByteArray from ImageMetaData() to Bitmap?

I have this code:
Frame frame = mSession.update();
Camera camera = frame.getCamera();
...
bytes=frame.getImageMetadata().getByteArray(0);
System.out.println("Byte Array "+frame.getImageMetadata().getByteArray(0));
Bitmap bmp = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
System.out.println(bmp);
When I print Bitmap, I get a null object. I'm trying to get the image from the camera, that's the reason I'm trying to convert byteArray to Bitmap. If there's an alternative way, it would also be helpful.
Thank You.
The ImageMetaData describes the background image, but does not actually contain the image itself.
If you want to capture the background image as a Bitmap, you should look at the computervision sample which uses a FrameBufferObject to copy the image to a byte array.
I've tried something similar. It works. But I don't recommend anyone to try this way. It takes time because of nested loops.
CameraImageBuffer inputImage;
final Bitmap bmp = Bitmap.createBitmap(inputImage.width, inputImage.height, Bitmap.Config.ARGB_8888);
int width = inputImage.width;
int height = inputImage.height;
int frameSize = width*height;
// Write Bytebuffer to byte[]
byte[] imageBuffer= new byte[inputImage.buffer.remaining()];
inputImage.buffer.get(imageBuffer);
int[] rgba = new int[frameSize];
for (int i = 0; i < height; i++){
for (int j = 0; j < width; j++) {
int r =imageBuffer[(i * width + j)*4 + 0];
int g =imageBuffer[(i * width + j)*4 + 1];
int b =imageBuffer[(i * width + j)*4 + 2];
rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r;
}
}
bmp.setPixels(rgba, 0, width , 0, 0, width, height);
Bytebuffer is converted to rgba buffer, and is written to Bitmap. CameraImageBuffer is the class provided in computervision sample app.
You may not able to get bitmap using image metadata. Use below approach.Use onDrawFrame override method of surface view render.
#Override public void onDrawFrame(GL10 gl) {
int w = 1080;
int h = 1080;
int b[] = new int[w * (0 + h)];
int bt[] = new int[w * h];
IntBuffer ib = IntBuffer.wrap(b);
ib.position(0);
GLES20.glReadPixels(0, 0, w, h, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, ib);
for (int i = 0, k = 0; i < h; i++, k++) {
for (int j = 0; j < w; j++) {
int pix = b[i * w + j];
int pb = (pix >> 16) & 0xff;
int pr = (pix << 16) & 0x00ff0000;
int pix1 = (pix & 0xff00ff00) | pr | pb;
bt[(h - k - 1) * w + j] = pix1;
}
}
Bitmap mBitmap = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
runOnUiThread(new Runnable() {
#Override public void run() {
image_test.setImageBitmap(resizedBitmap);
}
});
}

How to set background image fitable in blackberry application

I have written the following code here the background image is displaying but the image did not cover the full background
private Bitmap background;
int mWidth = Display.getWidth();
int mHeight = Display.getHeight();
public MyScreen()
{
// Set the displayed title of the screen
//backgroundBitmap = Bitmap.getBitmapResource("slidimage.png");
final Bitmap background = Bitmap.getBitmapResource("slidimage.png");
HorizontalFieldManager vfm = new HorizontalFieldManager(USE_ALL_HEIGHT | USE_ALL_WIDTH) {
public void paint(Graphics g) {
g.drawBitmap(0, 0,mWidth, mHeight, background, 0, 0);
super.paint(g);
}
};
add(vfm);
public static Bitmap resizeBitmap(Bitmap image, int width, int height)
{
int rgb[] = new int[image.getWidth()*image.getHeight()];
image.getARGB(rgb, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int rgb2[] = rescaleArray(rgb, image.getWidth(), image.getHeight(), width, height);
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
You can use the above method to resize the image
just pass the image to be resized and its width and height .
and the function will return the resized image .
where rescale Array is the below method
private static int[] rescaleArray(int[] ini, int x, int y, int x2, int y2)
{
int out[] = new int[x2*y2];
for (int yy = 0; yy < y2; yy++)
{
int dy = yy * y / y2;
for (int xx = 0; xx < x2; xx++)
{
int dx = xx * x / x2;
out[(x2 * yy) + xx] = ini[(x * dy) + dx];
}
}
return out;
}

blackberry image processing [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am very new to Blackberry development.
I am working with an Blackberry project.
In this project i want to change image effects and control.!
Image effects are sepia,sketch,grayscale,negative ,flip etc
and controls are brightness,contrast,hue etc
I have tried for flip image effect. following is code that i hav tried..
I got the output but it is overlapped with original image.
[image effect-flip output]
anybody can solve this issue??
Bitmap flip(Bitmap bitmap) {
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < bitmap.getHeight(); i++) {
for (int j = 0; j < bitmap.getWidth(); j++) {
int swap = argb[i * bitmap.getWidth() + j];
argb[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
}
}
//bitmap.setARGB(data, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
Graphics gr=new Graphics(bitmap);
gr.clear(0, 0, bitmap.getWidth(), bitmap.getHeight());
bitmap.setARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
return bitmap;
}
anybody can solve this issue??
You need to flip on a new argb (called argb_flip) as in:
public Bitmap flip(Bitmap bitmap) {
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
int[] argb_flip = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < bitmap.getHeight(); i++) {
for (int j = 0; j < bitmap.getWidth(); j++) {
int swap = argb[i * bitmap.getWidth() + j];
argb_flip[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
}
}
bitmap.setARGB(argb_flip, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
return bitmap;
}

Blackberry: how to flip a Bitmap upside down?

How to flip a Bitmap upside down?
(I need this for loading an OpenGL texture in another program).
Here is my failed try:
stripe.png (courtesy of Pitr#OpenClipart):
Flip.java:
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class Flip extends UiApplication {
public static void main(String args[]) {
Flip app = new Flip();
app.enterEventDispatcher();
}
public Flip() {
pushScreen(new MyScreen());
}
}
class MyScreen extends MainScreen {
static final Bitmap STRIPE = flip(Bitmap.getBitmapResource("stripe.png"));
public MyScreen() {
setTitle("Flip the bitmap");
add(new BitmapField(STRIPE));
add(new ButtonField("Hello world"));
}
static Bitmap flip(Bitmap bitmap) {
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < bitmap.getHeight(); i++) {
for (int j = 0; j < bitmap.getWidth(); j++) {
int swap = argb[i * bitmap.getWidth() + j];
argb[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
}
}
bitmap.setARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
return bitmap;
}
}
Try using this bit of code:
for (int y = 0; y < bitmap.getHeight() / 2; y++) {
int upper_row = bitmap.getWidth() * y;
int lower_row = bitmap.getWidth() * (bitmap.getHeight() - 1 - y);
for (int x = 0; x < bitmap.getWidth(); x++) {
int temp = argb[upper_row + x];
argb[upper_row + x] = argb[lower_row + x];
argb[lower_row + x] = temp;
}
}
public Bitmap flip(Bitmap bitmap) {
int[] argb = new int[bitmap.getWidth() * bitmap.getHeight()];
int[] argb_flip = new int[bitmap.getWidth() * bitmap.getHeight()];
bitmap.getARGB(argb, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < bitmap.getHeight(); i++) {
for (int j = 0; j < bitmap.getWidth(); j++) {
int swap = argb[i * bitmap.getWidth() + j];
argb_flip[(bitmap.getHeight() - 1 - i) * bitmap.getWidth() + j] = swap;
}
}
bitmap.setARGB(argb_flip, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
return bitmap;
}
Try this, surely it will help you to flip the image by 180 degrees.

how to change a color in an image programmatically?

I have a .PNG image with a transparent background and a drawing in it with a black color, how could I change the "black drawing color" in this image to any color i want programmatically; using rim 4.5 API ?
THANKS IN ADVANCE ....
I found the solution, here it is for those who are interested.
Bitmap colorImage(Bitmap image, int color) {
int[] rgbData= new int[image.getWidth() * image.getHeight()];
image.getARGB(rgbData,
0,
image.getWidth(),
0,
0,
image.getWidth(),
image.getHeight());
for (int i = 0; i < rgbData.length; i++) {
int alpha = 0xFF000000 & rgbData[i];
if((rgbData[i] & 0x00FFFFFF) == 0x00000000)
rgbData[i]= alpha | color;
}
image.setARGB(rgbData,
0,
image.getWidth(),
0,
0,
image.getWidth(),
image.getHeight());
return image;
}
You can parse the image RGBs searching for the black color and replace it with whatever color you desire.
You can read your PNG image to byte array and edit palette chunk.
This method is suitable only for PNG-8 images.
Here is my code:
public static Image createImage(String filename) throws Throwable
{
DataInputStream dis = null;
InputStream is = null;
try {
is = new Object().getClass().getResourceAsStream(filename);
dis = new DataInputStream(is);
int pngLength = dis.available();
byte[] png = new byte[pngLength];
int offset = 0;
dis.read(png, offset, 4); offset += 4; //‰PNG
dis.read(png, offset, 4); offset += 4; //....
while (true) {
//length
dis.read(png, offset, 4); offset += 4;
int length = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);
//chunk type
dis.read(png, offset, 4); offset += 4;
int type = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);
//chunk data
for (int i=0; i<length; i++) {
dis.read(png, offset, 1); offset += 1;
}
//CRC
dis.read(png, offset, 4); offset += 4;
int crc = (png[offset-1]&0xFF) | ((png[offset-2]&0xFF)<<8) | ((png[offset-3]&0xFF)<<16) | ((png[offset-4]&0xFF)<<24);
if (type == 0x504C5445) { //'PLTE'
int CRCStart = offset-4;
int PLTEStart = offset-4-length;
//modify PLTE chunk
for (int i=PLTEStart; i<PLTEStart+length; i+=3) {
png[i+0] = ...
png[i+1] = ...
png[i+2] = ...
}
int newCRC = crc(png, PLTEStart-4, length+4);
png[CRCStart+0] = (byte)(newCRC>>24);
png[CRCStart+1] = (byte)(newCRC>>16);
png[CRCStart+2] = (byte)(newCRC>>8);
png[CRCStart+3] = (byte)(newCRC);
}
if (offset >= pngLength)
break;
}
return Image.createImage(png, 0, pngLength);
} catch (Throwable e) {
throw e;
} finally {
MainCanvas.closeInputStream(dis);
MainCanvas.closeInputStream(is);
}
}

Resources