How to calculate "scanLegth" param from Bitmap.getRGB565(...) - java

I'm trying get bytes from bitmap in blackberry using the next method in Bitmap:
getRGB565(byte[] rgbData, int offset, int scanLength, int x, int y, int width, int height)
But i have read the params and i don't know how must i calculate scanLength:
scanLength - Width of a scanline (in bytes) within the data array.
Any idea?

Here scanLength is the full width of the original image, while width is the width of the rectangle you are copying from.
If you are copying the whole image it is the same, but if you are copying only a part of the image you'll have scanLength > width.
See also the Bitmap#getRGB565 javadoc

To get byte[] from Bitmap I used this: http://blackberry-digger.blogspot.com/2009/05/code-convert-bitmap-to-png-and-then.html and it worked fine.

Sorry it was too easy . In getARG is another example , it must be used usually with the same int width param

Related

imagej always shows black images

for an assignment I have to display an image using imagej in java. So I used the following code:
FloatProcessor abc=new FloatProcessor(imageSizeX,imageSizeY);
for (int i=0;i<imageSizeX;i++){
for(int j=0;j<imageSizeY;j++){
abc.putPixel(i, j, 100);
}
}
ImagePlus im=new ImagePlus("test",abc);
im.show();
but the Image I get is always completely black. Can you tell me what the mistake is?
It should at least be white if the value was 0 shouldn't it?
(FYI: imageSizeX=imageSizeY=256)
.putPixel uses the conversion Float.intBitsToFloat.
If you want a direct access to the pixels, you can use setf(int x, int y, float value).
Moreover, if you already have the pixels into an array, you can use the constructors to immediately set the pixel values FloatProcessor(int width, int height, int[] pixels).

Adding a picture to Word Document using Apache POI

I know it is already possible to add a picture to Word document using the
XWPFRun: addPicture(java.io.InputStream pictureData, int pictureType, java.lang.String filename, int width, int height) throws InvalidFormatException, java.io.IOException
method. However, I don't want my Picture to be resized. The resizing is not scaling the new images: it is always stretching them, making them useless.
Is there a way to insert image with original size, or scale them proportionally???
I had used Apache POI for quite some time and I don't think it's possible to add picture to a WORD doc without specifying height and width.
I always use following code to retrieve the size of a picture and scale them accordingly if needed.
BufferedImage bi = ImageIO.read(new File(filename));
int width = bi.getWidth();
int height = bi.getHeight();
You can use Apache POI ImageUtils as following:
Dimension dimension = ImageUtils.getImageDimension(imageInputStream, XWPFDocument.PICTURE_TYPE_JPEG);
double width = dimension.getWidth();
double height = dimension.getHeight();

java - how to make an image using setRGB?

i have 2D array to keep color component value :
p[pixel_value][red]
p[pixel_value][green]
p[pixel_value][blue]
i just dont know how to use them to make an image.
i read about setRGB, what i understand is i should mix all of them to become a RGBArray. then how to make it?
is it any better way to make an image without setRGB ? i need some explanation.
The method setRGB() can be used to set the color of a pixel of an already existing image. You can open an already existing image and set all the pixels of it, using the values stored in your 2D array.
You can do it like this:
BufferedImage img = ImageIO.read(new File("image which you want to change the pixels"));
for(int width=0; width < img.getWidth(); width++)
{
for(int height=0; height < img.getHeight(); height++)
{
Color temp = new Color(p[pixel_value][red], p[pixel_value][green], p[pixel_value][blue]);
img.setRGB(width, height, temp.getRGB());
}
}
ImageIO.write(img, "jpg", new File("where you want to store this new image"));
Like this, you can iterate over all the pixels and set their color accordingly.
NOTE: By doing this, you will lose your original image. This is just a way which I know.
What you need is a BufferedImage. Create a BufferedImage of type TYPE_3BYTE_BGR, if RGB is what you want, with a specified width and height.
QuickFact:
The BufferedImage subclass describes an Image with an accessible
buffer of image data.
Then, call the getRaster() method to get a WritableRaster
QuickFact:
This class extends Raster to provide pixel writing capabilities.
Then, use the setPixel(int x, int y, double[] dArray) method to set the pixels.
Update:
If all you want is to read an image, use the ImageIO.read(File f) method. It will allow you to read an image file in just one method call.
Somewhat SSCCE:
BufferedImage img = null;
try {
img = ImageIO.read(new File("strawberry.jpg"));
} catch (IOException e) {
}
You want to manually set RGB values?
You need to know that since an int is 32bit it contains all 4 rgb values (1 for the transparency).
xxxxxxxx xxxxxxxx xxxxxxxx xxxxxxxx
^Alpha ^red ^green ^blue
You can accomplish using 4 int values by the use of binary arithmetic:
int rgb = (red << 16) && () (green << 8) & (blue);
bufferedImage.setRGB(x, y, rgb);
IN the above you can add Alpha as well if needed. You just "push" the binary codes into the right places.

How to scale text on a texture (quad object)?

I have found a good way to draw text in openGL by creating a Bitmap, drawing text on it, and making it into a texture.
I have screenCoordinates(320,~~480) and gameCoordinates(20,28), in which quads' dimensions are defined. Example constructor: GameObject(x, y, width, height, text).
To create the bitmap I create a Canvas with this size:
// get width and height in px (p is a Paint() object)
p.setTextSize(textSize * Main.getMainResources().getDisplayMetrics().density);
final int width = (int) p.measureText(text);
final int height = (int) (p.descent() + -p.ascent());
canvas.drawText(text, 0, -p.ascent(), p);
Create a bitmap of this, scaled to the next power of 2, and you have your texture-bitmap.
However the problem is that because the quad's dimensions are not adapted to the bitmap(text) dimensions, the text isn't very smooth. I have the possibility to change the quad's dimensions with quad.setDimensions(width, height), but what would be the size I'd have to give it, and also shouldn't I also have to worry about the textSize(sp) of the text i'm drawing?
This all is in 2D, and some simple math's might get involved, but how should I get the right dimensions?

Convert jpeg/png to an array of pixels in java

How can I convert a string containing a jpeg or png to an array (preferably one dimensional) of pixels? Ideally using classes built into java?
It turns out you need commons-fileupload. Look at the user guide for how to obtain the image InputStream. From there you can simply call:
BufferedImage image = ImageIO.read(item.getInputStream());
From here on there are many ways:
loop over the image dimensions and for each x and y call int rgb = image.getRGB(x, y);
same as above, but call getRed(x, y), getGreen(x, y), getBlue(x, y)
get the ColorModel and call the above methods there
call getRGB(startX, startY, w, h, rgbArray, offset, scansize)
call getData(), which returns a Raster, and call getPixes(..) there
Use PixelGraber. It returns one-dimensional array of RGB data.

Categories

Resources