Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Filters, Save Photo, Import Photo & Rotation #49

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.example.filters"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
172 changes: 168 additions & 4 deletions example/src/main/java/com/example/filters/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
package com.example.filters;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.zomato.photofilters.SampleFilters;
import com.zomato.photofilters.imageprocessors.Filter;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ThumbnailCallback {
Expand All @@ -24,6 +35,9 @@ public class MainActivity extends AppCompatActivity implements ThumbnailCallback
private Activity activity;
private RecyclerView thumbListView;
private ImageView placeHolderImageView;
private Uri imageUri;
private Bitmap bitmap;
private Bitmap filteredBitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -36,8 +50,60 @@ protected void onCreate(Bundle savedInstanceState) {
private void initUIWidgets() {
thumbListView = (RecyclerView) findViewById(R.id.thumbnails);
placeHolderImageView = (ImageView) findViewById(R.id.place_holder_imageview);
placeHolderImageView.setImageBitmap(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.photo), 640, 640, false));
bitmap = BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.photo);
placeHolderImageView.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 640, 640, false));
initHorizontalList();
ImageView import_photo = (ImageView) findViewById(R.id.import_photo);
ImageView rotate_left = (ImageView) findViewById(R.id.rotate_left);
ImageView rotate_right = (ImageView) findViewById(R.id.rotate_right);
ImageView save_photo = (ImageView) findViewById(R.id.save_photo);
assert save_photo != null;
save_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
save_photo();
}
});

assert import_photo != null;
import_photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
import_photo();
}
});

assert rotate_right != null;
rotate_right.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(filteredBitmap, filteredBitmap.getWidth(), filteredBitmap.getHeight(), true);
filteredBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
placeHolderImageView.setImageBitmap(filteredBitmap);
}
});

assert rotate_left != null;
rotate_left.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Matrix matrix = new Matrix();
matrix.postRotate(-90);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(filteredBitmap, filteredBitmap.getWidth(), filteredBitmap.getHeight(), true);
filteredBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
bitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
placeHolderImageView.setImageBitmap(filteredBitmap);
}
});
}

private void initHorizontalList() {
Expand All @@ -54,20 +120,24 @@ private void bindDataToAdapter() {
Handler handler = new Handler();
Runnable r = new Runnable() {
public void run() {
Bitmap thumbImage = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.photo), 640, 640, false);
Bitmap thumbImage = Bitmap.createScaledBitmap(bitmap, placeHolderImageView.getDrawable().getIntrinsicWidth(), placeHolderImageView.getDrawable().getIntrinsicHeight(), false);
ThumbnailItem t1 = new ThumbnailItem();
ThumbnailItem t2 = new ThumbnailItem();
ThumbnailItem t3 = new ThumbnailItem();
ThumbnailItem t4 = new ThumbnailItem();
ThumbnailItem t5 = new ThumbnailItem();
ThumbnailItem t6 = new ThumbnailItem();
ThumbnailItem t7 = new ThumbnailItem();
ThumbnailItem t8 = new ThumbnailItem();

t1.image = thumbImage;
t2.image = thumbImage;
t3.image = thumbImage;
t4.image = thumbImage;
t5.image = thumbImage;
t6.image = thumbImage;
t7.image = thumbImage;
t8.image = thumbImage;
ThumbnailsManager.clearThumbs();
ThumbnailsManager.addThumb(t1); // Original Image

Expand All @@ -86,6 +156,12 @@ public void run() {
t6.filter = SampleFilters.getNightWhisperFilter();
ThumbnailsManager.addThumb(t6);

t7.filter = SampleFilters.getMonoChromeFilter();
ThumbnailsManager.addThumb(t7);

t8.filter = SampleFilters.getVioletFilter();
ThumbnailsManager.addThumb(t8);

List<ThumbnailItem> thumbs = ThumbnailsManager.processThumbs(context);

ThumbnailsAdapter adapter = new ThumbnailsAdapter(thumbs, (ThumbnailCallback) activity);
Expand All @@ -97,7 +173,95 @@ public void run() {
}

@Override
public void onThumbnailClick(Filter filter) {
placeHolderImageView.setImageBitmap(filter.processFilter(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(this.getApplicationContext().getResources(), R.drawable.photo), 640, 640, false)));
public void onThumbnailClick(Filter filter)
{
filteredBitmap = filter.processFilter(Bitmap.createScaledBitmap(bitmap, placeHolderImageView.getDrawable().getIntrinsicWidth(), placeHolderImageView.getDrawable().getIntrinsicHeight(), false));
placeHolderImageView.setImageBitmap(filteredBitmap);
}

private void save_photo()
{
Bitmap filteredBitmap = ((BitmapDrawable) placeHolderImageView.getDrawable()).getBitmap();
MediaStore.Images.Media.insertImage(getContentResolver(), filteredBitmap,
"photo_" + System.currentTimeMillis(), "");
Toast.makeText(MainActivity.this, "saved in Pictures", Toast.LENGTH_SHORT).show();
}

private void import_photo()
{
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose Photo");
builder.setItems(options, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int item)
{
if (options[item].equals("Take Photo"))
{
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "New Picture");
values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
startActivityForResult(takePictureIntent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, 2);
}
else if (options[item].equals("Cancel"))
{
dialog.dismiss();
}
}
});
builder.show();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
try
{
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
placeHolderImageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
assert bitmap != null;
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888 , true);
filteredBitmap = bitmap;
bindDataToAdapter();
}
else if (requestCode == 2)
{
imageUri = data.getData();
placeHolderImageView.setImageURI(imageUri);
try
{
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
}
catch (IOException e)
{
e.printStackTrace();
}
assert bitmap != null;
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888 , true);
filteredBitmap = bitmap;
bindDataToAdapter();
}
}
}
}
Binary file added example/src/main/res/drawable/import_photo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/src/main/res/drawable/rotate_left.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/src/main/res/drawable/rotate_right.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/src/main/res/drawable/save_photo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/>

<RelativeLayout
android:id="@+id/rel1"
android:layout_width="match_parent"
android:layout_height="@dimen/recycler_size"
android:layout_alignParentBottom="true"
Expand All @@ -33,4 +34,49 @@

</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_above="@+id/rel1"
android:background="@color/thumb_background_color">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/import_photo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="30dp"
android:src="@drawable/import_photo"/>
<ImageView
android:id="@+id/rotate_left"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toEndOf="@id/import_photo"
android:src="@drawable/rotate_left"
android:layout_toRightOf="@id/import_photo"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"/>
<ImageView
android:id="@+id/rotate_right"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toEndOf="@id/rotate_left"
android:src="@drawable/rotate_right"
android:layout_toRightOf="@id/rotate_left"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"/>
<ImageView
android:id="@+id/save_photo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_toEndOf="@id/rotate_right"
android:src="@drawable/save_photo"
android:layout_toRightOf="@id/rotate_right"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp" />
</RelativeLayout>
</RelativeLayout>

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.zomato.photofilters.geometry.Point;
import com.zomato.photofilters.imageprocessors.Filter;
import com.zomato.photofilters.imageprocessors.subfilters.BrightnessSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ColorOverlaySubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ContrastSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.SaturationSubFilter;
import com.zomato.photofilters.imageprocessors.subfilters.ToneCurveSubFilter;

/**
Expand Down Expand Up @@ -135,4 +137,36 @@ public static Filter getNightWhisperFilter() {
filter.addSubFilter(new ToneCurveSubFilter(rgbKnots, redKnots, greenKnots, blueKnots));
return filter;
}

public static Filter getMonoChromeFilter() {

Filter filter = new Filter();
// filter.addSubFilter(new ToneCurveSubFilter(rgbKnots, redKnots, greenKnots, blueKnots));
// filter.addSubFilter(new BrightnessSubFilter(-100));
filter.addSubFilter(new SaturationSubFilter(-10));

// filter.addSubFilter(new ContrastSubFilter(2f));
// filter.addSubFilter(new ColorOverlaySubFilter(4, 10F, 10F, 10F));
return filter;
}

public static Filter getVioletFilter()
{
Point[] greenKnots;
greenKnots = new Point[8];
greenKnots[0] = new Point(0, 0);
greenKnots[1] = new Point(86, 34);
greenKnots[2] = new Point(117, 41);
greenKnots[3] = new Point(146, 80);
greenKnots[4] = new Point(170, 151);
greenKnots[5] = new Point(200, 214);
greenKnots[6] = new Point(225, 242);
greenKnots[7] = new Point(255, 255);
Filter filter = new Filter();
filter.addSubFilter(new SaturationSubFilter((float) -1));
filter.addSubFilter(new ColorOverlaySubFilter(80,(float) 0.1,0,(float) 0.9));
filter.addSubFilter(new BrightnessSubFilter(-30));
filter.addSubFilter(new ToneCurveSubFilter(null,null , greenKnots, null));
return filter;
}
}