Android combining multiple images from database to single bitmap - android

i need to combine multiple images to a single bitmap. i need to combine the images in the range from 2-6. The path of the image is taken from my app's database and image is stored in my external sd card.
i have combined 2 images . But i need to combine 3, 4, 5 and 6 images in to single canvas.
My code for combining 2 images is provided below:-
private Bitmap combineImageIntoOne(ArrayList<Bitmap> a) {
int top = 0;
int width = 0, height = 0;
for (int j = 0; j < a.size(); j++) {
width += a.get(j).getWidth();
}
height = a.get(0).getHeight();
Bitmap combineBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(combineBitmap);
for (int i = 0; i < a.size(); i++) {
System.out.println("image width = " + top);
comboImage.drawBitmap(a.get(i), top, 0f, null);
top = top + a.get(i).getWidth();
}
System.out.println("combineBitmap combineBitmap..in.."+combineBitmap);
return combineBitmap;
}

I think you can combine 3, 4, 5 and 6 images in to single canvas by the same code.
If you want to use the tallest bitmap's height,you can change the code like this.
//get the max height
height = a.get(0).getHeight();
for (int j = 0; j < a.size(); j++) {
width += a.get(j).getWidth();
if(height<a.get(j).getHeight()){
height=a.get(j).getHeight();
}
}

Related

OpenCV convert color per pixel

Hello i want to convert the color in image, i'm using per-pixel methods but it seems very slow
src.getPixels(pixels, 0, width, 0, 0, width, height);
// RGB values
int R;
for (int i = 0; i < pixels.length; i++) {
// Get RGB values as ints
// Set pixel color
pixels[i] = color;
}
// Set pixels
src.setPixels(pixels, 0, width, 0, 0, width, height);
my question, is there any way i can do it using openCV? change pixel to the color i want ?
I recommend this excellent article on how to access/modify an opencv image buffer. I recommend
"the efficient way":
int i,j;
uchar* p;
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];
}
Or "the iterator-safe method":
MatIterator_<Vec3b> it, end;
for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
{
(*it)[0] = table[(*it)[0]];
(*it)[1] = table[(*it)[1]];
(*it)[2] = table[(*it)[2]];
}
For further optimizations, using cv::LUT() (where possible) can give huge speedups, but it is more intensive to design/code.
You can access Pixels by using:
img.at<Type>(y, x);
So to change an RGB Value you can use:
// read color
Vec3b intensity = img.at<Vec3b>(y, x);
// compute new color using intensity.val[0] etc. to access color values
// write new color
img.at<Vec3b>(y, x) = intensity;
#Boyko mentioned an Article from OpenCV concerning fast access to the image pixels if you want to iterate over all Pixel. The Method I would prefer from this Article is the iterator Method, as it is only slightly slower than direct pointer access but safer to use.
Example Code:
Mat& AssignNewColors(Mat& img)
{
// accept only char type matrices
CV_Assert(img.depth() != sizeof(uchar));
const int channels = img.channels();
switch(channels)
{
// case 1: skipped here
case 3:
{
// Read RGG Pixels
Mat_<Vec3b> _img = img;
for( int i = 0; i < img.rows; ++i)
for( int j = 0; j < img.cols; ++j )
{
_img(i,j)[0] = computeNewColor(_img(i,j)[0]);
_img(i,j)[1] = computeNewColor(_img(i,j)[1]);
_img(i,j)[2] = computeNewColor(_img(i,j)[2]);
}
img = _img;
break;
}
}
return img;
}

how to get pixel color using byte array in Android

In my Android project,
Here is my code.
for (int x = 0; x < targetBitArray.length; x += weight) {
for (int y = 0; y < targetBitArray[x].length; y += weight) {
targetBitArray[x][y] = bmp.getPixel(x, y) == mSearchColor;
}
}
but this code wastes a lot of time.
So I need to find way faster than bitmap.getPixel().
I'm trying to get pixel color using byte array converted from bitmap, but I can't.
How to replace Bitmap.getPixel()?
Each Bitmap.getPixel method invocation requires a lot of resources, so you need to avoid the amount of requests in order to improve the performace of your code.
My suggestion is:
Read the image data row-by-row with Bitmap.getPixels method into a local array
Iterate along your local array
e.g.
int [] rowData= new int [bitmapWidth];
for (int row = 0; row < bitmapHeight; row ++) {
// Load row of pixels
bitmap.getPixels(rowData, 0, bitmapWidth, 0, row, bitmapWidth, 1);
for (int column = 0; column < bitmapWidth; column ++) {
targetBitArray[column][row] = rowData(column) == mSearchColor;
}
}
This will be a great improvement for the performace of your code

Get pixels from bitmap, display them results in wrong order

I just wrote some code which grabs an image, stores it's pixels into an array and displays every pixel again, so that finally the grabbed image should be shown.
I took following image:
It's an 32x32 png image that I chose to use as an example.
That's my output:
As You might see, the result has far more pixels than 32x32.
Here's how my code looks:
First, an array was created that should get all the pixel informations of the image:
private Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.test);
private int w = b.getWidth;
private int h = b.getHeight;
private int pixels = w*h;
Then I used a method to store the bitmap's pixels into the array:
b.getPixels(pixels, 0, w, 0, 0, w, h);
I thought this should be it, now I can draw the pixels to my View:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++) {
p.setColor(Color.parseColor(String.format("#%06X", (0xFFFFFF & pixelImage.pixels[i+i*j]))));
canvas.drawRect(i, j, i+1, j+1, paint);
}
}
}
I have no idea where my error is, maybe somebody can help me?
Your algorithm is wrong, and frequently a bit wtf-y. There's no reason for String.format to even come into it, I'm not even sure where you're going there. And drawRect is not the way to do it. Try:
for(int i = 0; i < w; i++) {
for(int j = 0; j < h; j++) {
p.setColor(pixels[i*bmp.getWidth()+j]);
canvas.drawPoint(j, i, paint);
}
}

sprite size libgdx android

I created 5 sprites using libgdx library, then want to assign the different size to all of them between 0 to 10 and then move from left to right. 5 sprite is succussfully created but the problem is in the size. i tried the S.nextInt(10). but its not working.
Also i want to create all the sprites between 100-200 height. how to do it?
int number = 5;
sprite = new Sprite[number];
for (int i = 0; i < number; i++) {
sprite[i] = new Sprite(texture);
int size = S.nextInt(10);
sprite[i].setSize(size, size);
speed = S.nextInt(2);
sprite[i].setPosition(speed, 100);
}
batch.begin();
for (int i = 0; i < number; i++) {
sprite[i].draw(batch);
}
batch.end();

How to detect color line in image using android java

If I have an image containing black lines, like the image below. How can I detect the black lines.( to know the x,y of dots on this lines) ?
I want to know how to get started with this topic. What library should I learn to go through
You can use Catalano Framework.
http://code.google.com/p/catalano-framework/
FastBitmap image = new FastBitmap(bitmap);
image.toGrayscale();
int width = image.getWidth();
int height = image.getHeight();
ArrayList<IntPoint> lst = new ArrayList<>();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (image.getGray(i, j) == 0)
lst.add(new IntPoint(i,j));
}
}

Categories

Resources