Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Water Reflection to Image Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 441
    Comment on it

    This blog is about how to create water reflection image.

    1. Create android Project.

    2. Activity layout file is look like :-

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.ammy.waterreflection.MainActivity">
    
        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
    

    3. Write Image.java class.
     

    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    
    import java.nio.IntBuffer;
    
    /**
     *
     */
    public class Image {
        //original bitmap image
        public Bitmap image;
        public Bitmap destImage;
        
        //format of image (jpg/png)
        private String formatName;
        //dimensions of image
        private int width, height;
        // RGB Array Color
        protected int[] colorArray;
        
        public Image(Bitmap img){
            this.image =  img;
            formatName = "jpg";
            width = img.getWidth();
            height = img.getHeight();
            destImage = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    
            updateColorArray();
        }
        
        public Image clone(){
        	return new Image(this.image);
        }
        
        /**
         * Set color array for image - called on initialisation
         * by constructor
         * @param
         */
        private void updateColorArray(){
            colorArray = new int[width * height];
            image.getPixels(colorArray, 0, width, 0, 0, width, height);
            int r, g, b;
            for (int y = 0; y < height; y++){
                for (int x = 0; x < width; x++){
                    int index = y * width + x;
                    r = (colorArray[index] >> 16) & 0xff;
                    g = (colorArray[index] >> 8) & 0xff;
                    b = colorArray[index] & 0xff;
                    colorArray[index] = 0xff000000 | (b << 16) | (g << 8) | r;
                }
            }               
        }
    
        /**
         * Set the color of a specified pixel from an RGB combo
         */
        public void setPixelColor(int x, int y, int c0, int c1, int c2){
    	    int rgbcolor = (255 << 24) + (c0 << 16) + (c1 << 8) + c2;
            colorArray[((y*image.getWidth()+x))] = rgbcolor;
        }
        
        public void copyPixelsFromBuffer() {
        	IntBuffer vbb = IntBuffer.wrap(colorArray);
            destImage.copyPixelsFromBuffer(vbb);
            vbb.clear();
        }
        
        /**
         * Method to get the RED color for the specified 
         * pixel
         * @return color of R
         */
        public int getRComponent(int x, int y){
             return (getColorArray()[((y*width+x))]& 0x00FF0000) >>> 16;
        }
    
        /**
         * Method to get the GREEN color for the specified 
         * pixel
         * @return color of G
         */
        public int getGComponent(int x, int y){
             return (getColorArray()[((y*width+x))]& 0x0000FF00) >>> 8;
        }
    
        /**
         * Method to get the BLUE color for the specified 
         * pixel
         * @return color of B
         */
        public int getBComponent(int x, int y){
             return (getColorArray()[((y*width+x))] & 0x000000FF);
        }
    
        /**
         * @return the image
         */
        public Bitmap getImage() {
        	return destImage;
        }
    
        /**
         * @return the width
         */
        public int getWidth() {
            return width;
        }
    
        /**
         * @return the height
         */
        public int getHeight() {
            return height;
        }
    
        /**
         * @return the colorArray
         */
        public int[] getColorArray() {
            return colorArray;
        }
    
    }

    4. Create new class WaterReflection.java and copy below code:-

    public class WaterReflection {
    
    	int   _amount ;
    	
    	/**
        amount >= 2.
    	*/
    	public WaterReflection(int amount)
    	{
    	    _amount = ((amount >= 2) ? amount : 2) ;
    	}
    
        public Image process(Image imageIn) {
        	int r, g, b, m_current = 0;
    		  int width = imageIn.getWidth();
    		  int height = imageIn.getHeight();
    		  Image clone = imageIn.clone();
    		  for(int y = 0 ; y < height ; y++){
    			  for(int x = 0 ; x < width ; x++){
    				   if (x == 0) {
    					   m_current = (getRandomInt(-255, 0xff) % _amount) * ((getRandomInt(-255, 0xff) % 2 > 0) ? 1 : -1) ;
    				   }
    				   int sx = FClamp(x+m_current, 0, width-1);
    			       r = clone.getRComponent(sx, y);
    				   g = clone.getGComponent(sx, y);
    				   b = clone.getBComponent(sx, y);
    				   imageIn.setPixelColor(x, y, r, g, b);
    			  }
    		  }
            return imageIn;     
        }
    
    	public int getRandomInt(int a, int b) {
    		int min = Math.min(a, b);
    		int max = Math.max(a, b);
    		return min + (int)(Math.random() * (max - min + 1));
    	}
    
    	public static int FClamp(final int t, final int tLow, final int tHigh)
    	{
    		if (t < tHigh)
    		{
    			return ((t > tLow) ? t : tLow) ;
    		}
    		return tHigh ;
    	}
    
    }

    5.  On your Activity write field variables.

    Bitmap bit;
    private Bitmap blurBitmap;
    
    private ImageView image;

     

    6.  write below code on your OnCreate().

     bit = BitmapFactory.decodeResource(getResources(), R.drawable.abc);
            image = (ImageView) findViewById(R.id.image);
    
    
                processImageTask processImage = new processImageTask(new WaterReflection(30));
                processImage.execute();

    *Note:- I use image from drawable get bitmap object. you can use gallery/Camera intent and process bitmap.

     

    Result:-

    Happy Coding :D

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: