Wednesday 22 August 2012

Image (Split and merge)

This blog is useful to split and merge image in android.
This application will for merging number of smaller chunks to make the origina.So for that we need.some smaller image chunks. here  programmatically first split an image to get the smaller image chunks
and then merge them to get the original one.
         
    If you want you can also get some smaller chunks and directly merge them.    
        The below 4 lines of code does the splitting operation.   
        
        ImageView image = new ImageView(getApplicationContext());
        image.setImageResource(R.drawable.star);
        int chunkNumbers = 25;
        smallImages = splitImage(image, chunkNumbers);


Here as given below code you can split image .
in spliteImage() method pass ImageView object and chunkNumber.
chunkNumber = chunkNumbers is to tell how many chunks the image should split.
This method return ArrayList<Bitmap> of bitmap which set into grid view.

private void splitImage(ImageView image, int chunkNumbers) {
//For the number of rows and columns of the grid to be displayed
int rows,cols;
//For height and width of the small image chunks
int chunkHeight,chunkWidth;
//To store all the small image chunks in bitmap format in this list
ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers);
//Getting the scaled bitmap of the source image
BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
rows = cols = (int) Math.sqrt(chunkNumbers);
chunkHeight = bitmap.getHeight()/rows;
chunkWidth = bitmap.getWidth()/cols;
//xCoord and yCoord are the pixel positions of the image chunks
int yCoord = 0;
for(int x=0; x<rows; x++){
int xCoord = 0;
for(int y=0; y<cols; y++){
chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, chunkHeight));
xCoord += chunkWidth;
}
yCoord += chunkHeight;
}
}

      Result is shown below. 


Alos we can merge image by using this method

Bitmap bitmap = Bitmap.createBitmap(chunkWidth * 5, chunkHeight * 5, Bitmap.Config.ARGB_4444);
//create a canvas for drawing all those small images
Canvas canvas = new Canvas(bitmap);
int count = 0;
for(int rows = 0; rows < 5; rows++){
for(int cols = 0; cols < 5; cols++){
canvas.drawBitmap(smallImages.get(count), chunkWidth * cols, chunkHeight * rows, null);
count++;
}
}

here, bitmap which set into imageview as shown below.
ImageView image = (ImageView) findViewById(R.id.source_image);
image.setImageBitmap(bitmap);
          Result is shown below.


Monday 20 August 2012

Merge Images in Android


Merge two or more images in android by using Canvas its simple to merge image by using below code,
first create bitmap for particular image which you want to merge it.

get X and Y axis position for which area you want to merge images.

        mComboImage = new Canvas(mBackground);

       mComboImage.drawBitmap(c, x-axis position in f, y-axis position in f, null);

        mComboImage.drawBitmap(c, 0f, 0f, null);
        mComboImage.drawBitmap(s, 200f, 200f, null);
      
        
        mBitmapDrawable = new BitmapDrawable(mBackground);
        Bitmap mNewSaving = ((BitmapDrawable)mBitmapDrawable).getBitmap();


set this new bitmap in imageview. 
imageView.setImageBitmap(mNewSaving);


Here in this method two image bitmap combine in one bitmap which return bitmap of new merge image.Also save this image on sdcard.As  below code


public Bitmap combineImages(Bitmap c, Bitmap s) {
    Bitmap cs = null
 
    int width, height = 0; 
     
    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth(); 
      height = c.getHeight() + s.getHeight(); 
    } else
      width = s.getWidth(); 
      height = c.getHeight() + s.getHeight(); 
    } 
 
    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
 
    Canvas comboImage = new Canvas(cs); 
    comboImage.drawBitmap(c, new Matrix(), null);
    comboImage.drawBitmap(s, new Matrix(), null);

 
    // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
    /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 
 
    OutputStream os = null; 
    try { 
      os = new FileOutputStream(loc + tmpImg); 
      cs.compress(CompressFormat.PNG, 100, os); 
    } catch(IOException e) { 
      Log.e("combineImages", "problem combining images", e); 
    }*/ 
 
    return cs; 
  } 
}


Stylish menu in Android

An android custom view which looks like the menu in Path 2.0 (for iOS).
I try to realize the stylish menu for android, which could be equal to the iOS version's.

To setup the menu:


CircleMenu circleMenu = (CircleMenu) findViewById(R.id.circle_menu);
for (int i = 0; i < itemCount; i++) {
ImageView item = new ImageView(this);
item.setImageResource(ITEM_DRAWABLES[i]);
final int position = i;
circleMenu.addItem(item, new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "position:" + position, Toast.LENGTH_SHORT).show();
}
});// Add a menu item
}


Copy and share your useful data from Google chrome browser to your application in android.

Here In this blog I explain that how to copy and share your useful data from chrome browser in android. 1) How to show your applic...