Image Manipulation in Andriod

in #utopian-io6 years ago (edited)

397ea7d81434b7f308cf3eb357ffc2bb.jpg

Android enables you to control pictures by including various types of impacts the pictures. You can without much of a stretch apply picture handling strategies to include certain sorts of impacts pictures. The impacts could be brightness,darkness, grayscale change e.t.c.

Android gives Bitmap class to deal with pictures. This can be found under android.graphics.bitmap. There are numerous courses through which you can instantiate bitmap. We are making a bitmap of picture from the imageView.

     private Bitmap bmp;
        private ImageView img;
              img = (ImageView)findViewById(R.id.imageView1);
                    BitmapDrawable  abmp = (BitmapDrawable)img.getDrawable()

Presently we will make bitmap by calling getBitmap() capacity of BitmapDrawable class. Its sentence structure is given underneath −

                        bmp = abmp.getBitmap();

A picture is only a two dimensional framework. Same way you will deal with a bitmap. A picture comprise of pixels. So you will get pixels from this bitmap and apply handling to it. Its linguistic structure is as per the following −

    for(int i=0; i<bmp.getWidth(); i++){
      for(int j=0; j<bmp.getHeight(); j++){
           int p = bmp.getPixel(i, j);
          }
             }

The getWidth() and getHeight() capacities restores the stature and width of the network. The getPixel() technique restores the pixel at the predefined file. When you got the pixel, you can without much of a stretch control it as indicated by your necessities.

The beneath illustration shows a portion of the picture consequences for the bitmap. It cartons an essential application that enables you to change over the photo into grayscale and substantially more.

To try different things with this illustration , you have to run this on a genuine gadget.

Steps

  • open Andriod studio to create android app under a package com.example.faizaraja.myapplication
  • Modify src/MainActivity.java file to add necessary code
  • Modify the res/layout/activity_main to add respective XML components
  • Run the application and pick a running android gadget and introduce the application on it and confirm the outcomes
  • Following is the content of the modified MainActivity.java.

                                package com.example.faizaraja.myapplication;
    
                           import android.graphics.Bitmap;
                            import android.graphics.Color;
                        import android.graphics.drawable.BitmapDrawable;
                   import android.support.v7.app.ActionBarActivity;
    
                          import android.os.Bundle;
                                 import android.view.View;
                              import android.widget.Button;
                     import android.widget.ImageView;
    
                          public class MainActivity extends ActionBarActivity {
                          Button b1, b2, b3;
                          ImageView im;
    
                    private Bitmap bmp;
                                private Bitmap operation;
    
                         @Override
                        protected void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
      
                          b1 = (Button) findViewById(R.id.button);
                        b2 = (Button) findViewById(R.id.button2);
                           b3 = (Button) findViewById(R.id.button3);
                           im = (ImageView) findViewById(R.id.imageView);
      
                           BitmapDrawable abmp = (BitmapDrawable) im.getDrawable();
                      bmp = abmp.getBitmap();
                  }
          
                     public void gray(View view) {
                 operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
                  double red = 0.33;
                      double green = 0.59;
                       double blue = 0.11;
      
                         for (int i = 0; i < bmp.getWidth(); i++) {
                         for (int j = 0; j < bmp.getHeight(); j++) {
                      int p = bmp.getPixel(i, j);
                      int r = Color.red(p);
                        int g = Color.green(p);
                           int b = Color.blue(p);
            
                           r = (int) red * r;
                    g = (int) green * g;
                    b = (int) blue * b;
                       operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
                 }
                }
               im.setImageBitmap(operation);
              }
    
                           public void bright(View view){
                          operation= Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(),bmp.getConfig());
      
                 for(int i=0; i<bmp.getWidth(); i++){
                 for(int j=0; j<bmp.getHeight(); j++){
                    int p = bmp.getPixel(i, j);
                         int r = Color.red(p);
                    int g = Color.green(p);
                     int b = Color.blue(p);
                    int alpha = Color.alpha(p);
            
            r = 100  +  r;
            g = 100  + g;
            b = 100  + b;
            alpha = 100 + alpha;
             operation.setPixel(i, j, Color.argb(alpha, r, g, b));
               }
                  }
                    im.setImageBitmap(operation);
                 }
    
               public void dark(View view){
            operation= Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
              for(int i=0; i<bmp.getWidth(); i++){
             for(int j=0; j<bmp.getHeight(); j++){
            int p = bmp.getPixel(i, j);
            int r = Color.red(p);
            int g = Color.green(p);
            int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  r - 50;
            g =  g - 50;
            b =  b - 50;
            alpha = alpha -50;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
             }
            }
                  im.setImageBitmap(operation);
        }
    
                   public void gama(View view) {
               operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(),bmp.getConfig());
      
                 for(int i=0; i<bmp.getWidth(); i++){
                    for(int j=0; j<bmp.getHeight(); j++){
                         int p = bmp.getPixel(i, j);
                      int r = Color.red(p);
                         int g = Color.green(p);
                      int b = Color.blue(p);
            int alpha = Color.alpha(p);
            
            r =  r + 150;
            g =  0;
            b =  0;
            alpha = 0;
            operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
                  }
                 }
                im.setImageBitmap(operation);
                }
    
                      public void green(View view){
                 operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
      
                  for(int i=0; <bmp.getWidth(); i++){
                 for(int j=0; j<bmp.getHeight(); j++){
                    int p = bmp.getPixel(i, j);
             int r = Color.red(p);
                     int g = Color.green(p);
                        int b = Color.blue(p);
                    int alpha = Color.alpha(p);
            
                       r =  0;
                    g =  g+150;
                     b =  0;
                   alpha = 0;
                   operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
                }
                      }
                      im.setImageBitmap(operation);
                 }
    
                      public void blue(View view){
                       operation = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), bmp.getConfig());
                
                        for(int i=0; i<bmp.getWidth(); i++){
                      for(int j=0; j<bmp.getHeight(); j++){
                     int p = bmp.getPixel(i, j);
                    int r = Color.red(p);
                        int g = Color.green(p);
                       int b = Color.blue(p);
                      int alpha = Color.alpha(p);
            
                     r =  0;
                       g =  0;
                   b =  b+150;
                   alpha = 0;
                  operation.setPixel(i, j, Color.argb(Color.alpha(p), r, g, b));
         }
      }
                   im.setImageBitmap(operation);
     }
    

    Following is the modified content of the xml res/layout/activity_main.xml.

                                                     <?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:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
           android:paddingTop="@dimen/activity_vertical_margin"
            android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    
        <TextView
              android:layout_width="wrap_content"
           android:layout_height="wrap_content"
             android:id="@+id/textView"
                     android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                 android:textSize="30dp"
                android:text="Image Effects" />
      
                    <TextView
                      android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                       android:text="Tutorials Point"
                      android:id="@+id/textView2"
                      android:layout_below="@+id/textView"
               android:layout_centerHorizontal="true"
                      android:textSize="35dp"
                     android:textColor="#ff16ff01" />
      
                  <ImageView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/imageView"
                 android:layout_below="@+id/textView2"
                     android:layout_centerHorizontal="true"
                         android:src="@drawable/abc"/>
    
                           <Button
                     android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                     android:text="Gray"
               android:onClick="gray"
                       android:id="@+id/button"
                   android:layout_alignParentBottom="true"
                android:layout_alignParentLeft="true"
                   android:layout_alignParentStart="true"
                    android:layout_marginBottom="97dp" />
      
                      <Button
                  android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                         android:text="dark"
                     android:onClick="dark"
                 android:id="@+id/button2"
                    android:layout_alignBottom="@+id/button"
                    android:layout_alignParentRight="true"
                     android:layout_alignParentEnd="true" />
      
                 <Button
                   android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                   android:text="Bright"
                android:onClick="bright"
               android:id="@+id/button3"
               android:layout_alignTop="@+id/button2"
                 android:layout_centerHorizontal="true" />
      
              <Button
            android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                    android:text="Red"
                    android:onClick="gama"
                                   android:id="@+id/button4"
                    android:layout_below="@+id/button3"
                   android:layout_alignParentLeft="true"
                       android:layout_alignParentStart="true" />
      
              <Button
                  android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                android:text="Green"
               android:onClick="green"
                    android:id="@+id/button5"
                     android:layout_alignTop="@+id/button4"
                    android:layout_alignLeft="@+id/button3"
                   android:layout_alignStart="@+id/button3" />
      
                <Button
                      android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                       android:text="blue"
                    android:onClick="blue"
                  android:id="@+id/button6"
                   android:layout_below="@+id/button2"
                  android:layout_toRightOf="@+id/textView"
                   android:layout_toEndOf="@+id/textView" />
      
                     </RelativeLayout>
    

    How about we endeavor to run our application we simply altered. I expect you had made your AVD while doing condition setup. To run the application from Android studio, open one of your undertaking's action records and snap Run Eclipse Run Icon symbol from the toolbar. Android studio introduces the application on your AVD and begins it and if all is well with your setup and application

    It is working as intended. In the upcoming tutorials I will show you how you can do more cool stuff with wxPython so stay tuned for that!

    Sort:  

    Source
    There is reasonable evidence that this article has been spun, rewritten, or reworded. Repeatedly posting such content is considered spam.

    Spam is discouraged by the community, and may result in action from the cheetah bot.

    More information and tips on sharing content.

    If you believe this comment is in error, please contact us in #disputes on Discord

    Steem Flag Rewards mention comment has been approved! Thank you for reporting this abuse,@flugschwein categorized as plagiarism. This post was submitted via our Discord Community channel. Check us out on the following link!
    SFR Discord

    Thank you for your contribution.

    • The tutorial is poorly structured.
    • Doesn´t follow the template for tutorials. See the template here.
    • There is a lot of information on this subject, try to find something more innovative and contribute to the open source community.

    See in this link an example of a good tutorial.

    Your contribution has been evaluated according to Utopian rules and guidelines, as well as a predefined set of questions pertaining to the category.
    To view those questions and the relevant answers related to your post,Click here


    Need help? Write a ticket on https://support.utopian.io/.
    Chat with us on Discord.
    [utopian-moderator]

    Thanks for your reveiw. Will surely keep those points in mind. There wasn't any related tutorial that I made so I didn't knew what to put at the place of curriculum. Anyway, thanks for your time.