Hi , sometimes we need to resize the Image when we are using them as bitmaps. in this process we need to use them according to our customized sizes. i wrote a utility class in this contest, from where you can call these methods and you can use. it is easy to handle and easy to use. feel free to ask any question :) have a great day with BlackBerry RIM API'S
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.Color;
import net.rim.device.api.ui.Graphics;
public class ImageResigeUtility {
public Bitmap resizeBitmap(Bitmap image, int width, int height) {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// Need an array (for RGB, with the size of original image)
int rgb[] = new int[imageWidth * imageHeight];
// Get the RGB array of image into "rgb"
image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);
// Call to rescaleArray() function and obtain rgb2
int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);
// Create an image with that RGB array
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
private 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;
}
public EncodedImage resizeImage(EncodedImage image, int newWidth,
int newHeight) {
int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()),
Fixed32.toFP(newWidth));
int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()),
Fixed32.toFP(newHeight));
return image.scaleImage32(scaleFactorX, scaleFactorY);
}
public Bitmap createBitmap(String imagePath, int newWidth, int newHeight) {
EncodedImage image = EncodedImage.getEncodedImageResource(imagePath);
int widthNumerator = Fixed32.toFP(image.getWidth());
int widthDenominator = Fixed32.toFP(newWidth);
int scaleWidth = Fixed32.div(widthNumerator, widthDenominator);
int heightNumerator = Fixed32.toFP(image.getHeight());
int heightDenominator = Fixed32.toFP(newHeight);
int scaleHeight = Fixed32.div(heightNumerator, heightDenominator);
image = image.scaleImage32(scaleWidth, scaleHeight);
return image.getBitmap();
}
public static Graphics resizeGraphics(Graphics g, int left, int top,
Bitmap bitmap, int width, int hight, int crop)
{
int tmpw = 0, tmph = 0, dw = 0;
int rw = 0, rh = 0, dh = 0;
try {
g.drawBitmap(left, top, crop, crop, bitmap, 0, 0);
g.drawBitmap(left + width - crop, top, crop, crop, bitmap,
bitmap.getWidth() - crop, 0);
g.drawBitmap(left, top + hight - crop, crop, crop, bitmap, 0,
bitmap.getHeight() - crop);
g.drawBitmap(left + width - crop, top + hight - crop, crop, crop,
bitmap, bitmap.getWidth() - crop, bitmap.getHeight() - crop);
tmpw = (bitmap.getWidth() - (crop * 2));
rw = ((width - (crop * 2)) / tmpw);
if (((width - (crop * 2)) % tmpw) != 0) {
rw++;
dw = ((width - (crop * 2)) % tmpw);
} else
dw = tmpw;
if (rw >= 1) {
for (int i = 0; i < rw; i++) {
if (i == rw - 1) {
g.drawBitmap(left + crop + i * tmpw, top, dw, crop,
bitmap, crop, 0);
g.drawBitmap(left + crop + i * tmpw,
top + hight - crop, dw, crop, bitmap, crop,
bitmap.getHeight() - crop);
} else {
g.drawBitmap(left + crop + i * tmpw, top, tmpw, crop,
bitmap, crop, 0);
g.drawBitmap(left + crop + i * tmpw,
top + hight - crop, tmpw, crop, bitmap, crop,
bitmap.getHeight() - crop);
}
}
}
tmph = (bitmap.getHeight() - (crop * 2));
rh = ((hight - (crop * 2)) / tmph);
if (((hight - (crop * 2)) % tmph) != 0)
{
rh++;
dh = ((hight - (crop * 2)) % tmph);
} else
dh = tmph;
if (rh >= 1) {
for (int i = 0; i < rh; i++) {
if (i == rh - 1) {
g.drawBitmap(left, top + crop + i * tmph, crop, dh,
bitmap, 0, crop);
g.drawBitmap(left + width - crop,
top + crop + i * tmph, crop, dh, bitmap,
bitmap.getWidth() - crop, crop);
} else {
g.drawBitmap(left, top + crop + i * tmph, crop, tmph,
bitmap, 0, crop);
g.drawBitmap(left + width - crop,
top + crop + i * tmph, crop, tmph, bitmap,
bitmap.getWidth() - crop, crop);
}
}
}
for (int i = 0; i < rh; i++) {
for (int j = 0; j < rw; j++) {
if (i == rh - 1 && j == rw - 1) {
g.drawBitmap(left + crop + j * tmpw, top + crop + i
* tmph, dw, dh, bitmap, crop, crop);
} else {
if (i == rh - 1 || j == rw - 1) {
if (j == rw - 1) {
g.drawBitmap(left + crop + j * tmpw, top + crop
+ i * tmph, dw, tmph, bitmap, crop,
crop);
} else {
g.drawBitmap(left + crop + j * tmpw, top + crop
+ i * tmph, tmpw, dh, bitmap, crop,
crop);
}
} else {
g.drawBitmap(left + crop + j * tmpw, top + crop + i
* tmph, tmpw, tmph, bitmap, crop, crop);
}
}
}
}
} catch (Exception e) {
System.out.println("==>falt in argument==>" + e);
}
return g;
}
public static Bitmap ResizeTransparentBitmap(Bitmap bmpSrc,
int nWidth, int nHeight, int nFilterType, int nAspectRatio)
{
if(bmpSrc== null)
return null;
//Get the original dimensions of the bitmap
int nOriginWidth = bmpSrc.getWidth();
int nOriginHeight = bmpSrc.getHeight();
if(nWidth == nOriginWidth && nHeight == nOriginHeight)
return bmpSrc;
//Prepare a drawing bitmap and graphic object
Bitmap bmpOrigin = new Bitmap(nOriginWidth, nOriginHeight);
Graphics graph = Graphics.create(bmpOrigin);
//Create a line of transparent pixels for later use
int[] aEmptyLine = new int[nWidth];
for(int x = 0; x < nWidth; x++)
aEmptyLine[x] = 0x00000000;
//Create two scaled bitmaps
Bitmap[] bmpScaled = new Bitmap[2];
for(int i = 0; i < 2; i++)
{
//Draw the bitmap on a white background first,
//then on a black background
graph.setColor((i == 0) ? Color.WHITE : Color.BLACK);
graph.fillRect(0, 0, nOriginWidth, nOriginHeight);
graph.drawBitmap(0, 0, nOriginWidth, nOriginHeight, bmpSrc, 0, 0);
//Create a new bitmap with the desired size
bmpScaled[i] = new Bitmap(nWidth, nHeight);
if(nAspectRatio == Bitmap.SCALE_TO_FIT)
{
//Set the alpha channel of all pixels to 0
//to ensure transparency is
//applied around the picture, if needed by the transformation
for(int y = 0; y < nHeight; y++)
bmpScaled[i].setARGB(aEmptyLine, 0, nWidth,
0, y, nWidth, 1);
}
//Scale the bitmap
bmpOrigin.scaleInto(bmpScaled[i], nFilterType, nAspectRatio);
}
//Prepare objects for final iteration
Bitmap bmpFinal = bmpScaled[0];
int[][] aPixelLine = new int[2][nWidth];
//Iterate every line of the two scaled bitmaps
for(int y = 0; y < nHeight; y++)
{
bmpScaled[0].getARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
bmpScaled[1].getARGB(aPixelLine[1], 0, nWidth, 0, y, nWidth, 1);
//Check every pixel one by one
for(int x = 0; x < nWidth; x++)
{
//If the pixel was untouched (alpha channel still at 0),
//keep it transparent
if(((aPixelLine[0][x] >> 24) & 0xff) == 0)
aPixelLine[0][x] = 0x00000000;
else
{
//Compute the alpha value based on the difference
//of intensity in the red channel
int nAlpha = ((aPixelLine[1][x] >> 16) & 0xff) -
((aPixelLine[0][x] >> 16) & 0xff) + 255;
if(nAlpha == 0)
aPixelLine[0][x] = 0x00000000; //Completely transparent
else if(nAlpha >= 255)
aPixelLine[0][x] |= 0xff000000; //Completely opaque
else
{
//Compute the value of the each channel one by one
int nRed = ((aPixelLine[0][x] >> 16 ) & 0xff);
int nGreen = ((aPixelLine[0][x] >> 8 ) & 0xff);
int nBlue = (aPixelLine[0][x] & 0xff);
nRed = (int)(255 +
(255.0 * ((double)(nRed-255)/(double)nAlpha)));
nGreen = (int)(255 +
(255.0 * ((double)(nGreen-255)/(double)nAlpha)));
nBlue = (int)(255 +
(255.0 * ((double)(nBlue-255)/(double)nAlpha)));
if(nRed < 0) nRed = 0;
if(nGreen < 0) nGreen = 0;
if(nBlue < 0) nBlue = 0;
aPixelLine[0][x] = nBlue | (nGreen<<8) |
(nRed<<16) | (nAlpha<<24);
}
}
}
//Change the pixels of this line to their final value
bmpFinal.setARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
}
return bmpFinal;
}
}
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.Color;
import net.rim.device.api.ui.Graphics;
public class ImageResigeUtility {
public Bitmap resizeBitmap(Bitmap image, int width, int height) {
int imageWidth = image.getWidth();
int imageHeight = image.getHeight();
// Need an array (for RGB, with the size of original image)
int rgb[] = new int[imageWidth * imageHeight];
// Get the RGB array of image into "rgb"
image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);
// Call to rescaleArray() function and obtain rgb2
int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);
// Create an image with that RGB array
Bitmap temp2 = new Bitmap(width, height);
temp2.setARGB(rgb2, 0, width, 0, 0, width, height);
return temp2;
}
private 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;
}
public EncodedImage resizeImage(EncodedImage image, int newWidth,
int newHeight) {
int scaleFactorX = Fixed32.div(Fixed32.toFP(image.getWidth()),
Fixed32.toFP(newWidth));
int scaleFactorY = Fixed32.div(Fixed32.toFP(image.getHeight()),
Fixed32.toFP(newHeight));
return image.scaleImage32(scaleFactorX, scaleFactorY);
}
public Bitmap createBitmap(String imagePath, int newWidth, int newHeight) {
EncodedImage image = EncodedImage.getEncodedImageResource(imagePath);
int widthNumerator = Fixed32.toFP(image.getWidth());
int widthDenominator = Fixed32.toFP(newWidth);
int scaleWidth = Fixed32.div(widthNumerator, widthDenominator);
int heightNumerator = Fixed32.toFP(image.getHeight());
int heightDenominator = Fixed32.toFP(newHeight);
int scaleHeight = Fixed32.div(heightNumerator, heightDenominator);
image = image.scaleImage32(scaleWidth, scaleHeight);
return image.getBitmap();
}
public static Graphics resizeGraphics(Graphics g, int left, int top,
Bitmap bitmap, int width, int hight, int crop)
{
int tmpw = 0, tmph = 0, dw = 0;
int rw = 0, rh = 0, dh = 0;
try {
g.drawBitmap(left, top, crop, crop, bitmap, 0, 0);
g.drawBitmap(left + width - crop, top, crop, crop, bitmap,
bitmap.getWidth() - crop, 0);
g.drawBitmap(left, top + hight - crop, crop, crop, bitmap, 0,
bitmap.getHeight() - crop);
g.drawBitmap(left + width - crop, top + hight - crop, crop, crop,
bitmap, bitmap.getWidth() - crop, bitmap.getHeight() - crop);
tmpw = (bitmap.getWidth() - (crop * 2));
rw = ((width - (crop * 2)) / tmpw);
if (((width - (crop * 2)) % tmpw) != 0) {
rw++;
dw = ((width - (crop * 2)) % tmpw);
} else
dw = tmpw;
if (rw >= 1) {
for (int i = 0; i < rw; i++) {
if (i == rw - 1) {
g.drawBitmap(left + crop + i * tmpw, top, dw, crop,
bitmap, crop, 0);
g.drawBitmap(left + crop + i * tmpw,
top + hight - crop, dw, crop, bitmap, crop,
bitmap.getHeight() - crop);
} else {
g.drawBitmap(left + crop + i * tmpw, top, tmpw, crop,
bitmap, crop, 0);
g.drawBitmap(left + crop + i * tmpw,
top + hight - crop, tmpw, crop, bitmap, crop,
bitmap.getHeight() - crop);
}
}
}
tmph = (bitmap.getHeight() - (crop * 2));
rh = ((hight - (crop * 2)) / tmph);
if (((hight - (crop * 2)) % tmph) != 0)
{
rh++;
dh = ((hight - (crop * 2)) % tmph);
} else
dh = tmph;
if (rh >= 1) {
for (int i = 0; i < rh; i++) {
if (i == rh - 1) {
g.drawBitmap(left, top + crop + i * tmph, crop, dh,
bitmap, 0, crop);
g.drawBitmap(left + width - crop,
top + crop + i * tmph, crop, dh, bitmap,
bitmap.getWidth() - crop, crop);
} else {
g.drawBitmap(left, top + crop + i * tmph, crop, tmph,
bitmap, 0, crop);
g.drawBitmap(left + width - crop,
top + crop + i * tmph, crop, tmph, bitmap,
bitmap.getWidth() - crop, crop);
}
}
}
for (int i = 0; i < rh; i++) {
for (int j = 0; j < rw; j++) {
if (i == rh - 1 && j == rw - 1) {
g.drawBitmap(left + crop + j * tmpw, top + crop + i
* tmph, dw, dh, bitmap, crop, crop);
} else {
if (i == rh - 1 || j == rw - 1) {
if (j == rw - 1) {
g.drawBitmap(left + crop + j * tmpw, top + crop
+ i * tmph, dw, tmph, bitmap, crop,
crop);
} else {
g.drawBitmap(left + crop + j * tmpw, top + crop
+ i * tmph, tmpw, dh, bitmap, crop,
crop);
}
} else {
g.drawBitmap(left + crop + j * tmpw, top + crop + i
* tmph, tmpw, tmph, bitmap, crop, crop);
}
}
}
}
} catch (Exception e) {
System.out.println("==>falt in argument==>" + e);
}
return g;
}
public static Bitmap ResizeTransparentBitmap(Bitmap bmpSrc,
int nWidth, int nHeight, int nFilterType, int nAspectRatio)
{
if(bmpSrc== null)
return null;
//Get the original dimensions of the bitmap
int nOriginWidth = bmpSrc.getWidth();
int nOriginHeight = bmpSrc.getHeight();
if(nWidth == nOriginWidth && nHeight == nOriginHeight)
return bmpSrc;
//Prepare a drawing bitmap and graphic object
Bitmap bmpOrigin = new Bitmap(nOriginWidth, nOriginHeight);
Graphics graph = Graphics.create(bmpOrigin);
//Create a line of transparent pixels for later use
int[] aEmptyLine = new int[nWidth];
for(int x = 0; x < nWidth; x++)
aEmptyLine[x] = 0x00000000;
//Create two scaled bitmaps
Bitmap[] bmpScaled = new Bitmap[2];
for(int i = 0; i < 2; i++)
{
//Draw the bitmap on a white background first,
//then on a black background
graph.setColor((i == 0) ? Color.WHITE : Color.BLACK);
graph.fillRect(0, 0, nOriginWidth, nOriginHeight);
graph.drawBitmap(0, 0, nOriginWidth, nOriginHeight, bmpSrc, 0, 0);
//Create a new bitmap with the desired size
bmpScaled[i] = new Bitmap(nWidth, nHeight);
if(nAspectRatio == Bitmap.SCALE_TO_FIT)
{
//Set the alpha channel of all pixels to 0
//to ensure transparency is
//applied around the picture, if needed by the transformation
for(int y = 0; y < nHeight; y++)
bmpScaled[i].setARGB(aEmptyLine, 0, nWidth,
0, y, nWidth, 1);
}
//Scale the bitmap
bmpOrigin.scaleInto(bmpScaled[i], nFilterType, nAspectRatio);
}
//Prepare objects for final iteration
Bitmap bmpFinal = bmpScaled[0];
int[][] aPixelLine = new int[2][nWidth];
//Iterate every line of the two scaled bitmaps
for(int y = 0; y < nHeight; y++)
{
bmpScaled[0].getARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
bmpScaled[1].getARGB(aPixelLine[1], 0, nWidth, 0, y, nWidth, 1);
//Check every pixel one by one
for(int x = 0; x < nWidth; x++)
{
//If the pixel was untouched (alpha channel still at 0),
//keep it transparent
if(((aPixelLine[0][x] >> 24) & 0xff) == 0)
aPixelLine[0][x] = 0x00000000;
else
{
//Compute the alpha value based on the difference
//of intensity in the red channel
int nAlpha = ((aPixelLine[1][x] >> 16) & 0xff) -
((aPixelLine[0][x] >> 16) & 0xff) + 255;
if(nAlpha == 0)
aPixelLine[0][x] = 0x00000000; //Completely transparent
else if(nAlpha >= 255)
aPixelLine[0][x] |= 0xff000000; //Completely opaque
else
{
//Compute the value of the each channel one by one
int nRed = ((aPixelLine[0][x] >> 16 ) & 0xff);
int nGreen = ((aPixelLine[0][x] >> 8 ) & 0xff);
int nBlue = (aPixelLine[0][x] & 0xff);
nRed = (int)(255 +
(255.0 * ((double)(nRed-255)/(double)nAlpha)));
nGreen = (int)(255 +
(255.0 * ((double)(nGreen-255)/(double)nAlpha)));
nBlue = (int)(255 +
(255.0 * ((double)(nBlue-255)/(double)nAlpha)));
if(nRed < 0) nRed = 0;
if(nGreen < 0) nGreen = 0;
if(nBlue < 0) nBlue = 0;
aPixelLine[0][x] = nBlue | (nGreen<<8) |
(nRed<<16) | (nAlpha<<24);
}
}
}
//Change the pixels of this line to their final value
bmpFinal.setARGB(aPixelLine[0], 0, nWidth, 0, y, nWidth, 1);
}
return bmpFinal;
}
}
No comments:
Post a Comment