Images for Android Photo Filter Tutorials

+2 Bucky Roberts · January 10, 2015
/images/forum/upload/2015-01-10/cb2a53907ff4ad8644569fae756d92c5.jpg

/images/forum/upload/2015-01-10/5f9af359167f1daefbeeb7be3fd2e69b.png

package com.thenewboston.photofilter;

import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {

    ImageView buckysImageView;
    Drawable buckysFace;
    Bitmap bitmapImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buckysImageView = (ImageView) findViewById(R.id.buckysImageView);

        buckysFace = getResources().getDrawable(R.drawable.bucky);
        bitmapImage = ((BitmapDrawable) buckysFace).getBitmap();
        Bitmap newPhoto = invertImage(bitmapImage);
        buckysImageView.setImageBitmap(newPhoto);

        /*
        Drawable[] layers = new Drawable[2];
        layers[0] = getResources().getDrawable(R.drawable.bucky);
        layers[1] = getResources().getDrawable(R.drawable.dirty);
        LayerDrawable layerDrawable = new LayerDrawable(layers);
        buckysImageView.setImageDrawable(layerDrawable);
        */

        //Save the image to the users device
        MediaStore.Images.Media.insertImage(getContentResolver(), newPhoto, "title", "description");
    }

    //Invert a bitmap image
    public static Bitmap invertImage(Bitmap original){
        Bitmap finalImage = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());

        int A, R, G, B;
        int pixelColor;
        int height = original.getHeight();
        int width = original.getWidth();

        for(int y = 0; y < height; y++){
            for(int x = 0; x < width; x++){
                pixelColor = original.getPixel(x, y);
                A = Color.alpha(pixelColor);
                R = 255 - Color.red(pixelColor);
                G = 255 - Color.green(pixelColor);
                B = 255 - Color.blue(pixelColor);
                finalImage.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }
        return finalImage;
    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 nathan abraham · May 17, 2016
How do get all these source codes?
0 tokinonagare W. · September 7, 2015
layers[0] = ResourcesCompat.getDrawable(getResources(), R.drawable.avatar, null);
layers[1] = ResourcesCompat.getDrawable(getResources(), R.drawable.dirty, null);

Would solve the  deprecated problem!
0 Rip Van Winkle · August 18, 2015
Fun example!

I did have to make one change, since apparently the old version of getDrawable got deprecated as of API 22. (They replaced it with a new version of getDrawable with an additional parameter for "theme", and I suppose it must make everything look just beautiful, but the new version seems to only work on API 22 and up, which almost nobody has installed yet.) Anyway, there's a compatible version called ContextCompat.getDrawable which seems to solve the problem in general.

So if anyone sees getDrawable crossed out in their code, replace these two lines
        layers[0] = getResources().getDrawable(R.drawable.bucky);
       layers[1] = getResources().getDrawable(R.drawable.dirty);

with these two:
layers[0] = ContextCompat.getDrawable(this, R.drawable.bucky);
layers[1] = ContextCompat.getDrawable(this, R.drawable.dirty);

... and that should fix it.

Except I had one other problem with the last part of the exercise. Like Roman, I tried to find where the image had been saved, and couldn't find it anywhere. I thought that maybe it was because I was testing using the emulator, and not an actual device. But if the image was stored somewhere, I sure couldn't find it.
0 Roman Smirnov · February 23, 2015
I tried using the insertImage() method to add my photo to gallery 
MediaStore.Images.Media.insertImage(getContentResolver(), newPhoto, "title", "description"); 
I added the permission    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The image doesn't get added to the gallery nor does it appear in any place I can figure out on the device
It returns content://media/external/images/media/19990 as String.

Any ideas?
  • 1

Java / Android Development

107,159 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator