Skip to content

Commit

Permalink
preferneces and builder configure
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed May 13, 2016
1 parent 3c1af72 commit 0601176
Show file tree
Hide file tree
Showing 13 changed files with 225 additions and 19 deletions.
17 changes: 11 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.naman14.arcade" >
package="com.naman14.arcade">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".ArcadeApp"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".ArcadeApp">
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
android:theme="@style/AppTheme.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".SettingsActivity"
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
21 changes: 18 additions & 3 deletions app/src/main/java/com/naman14/arcade/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -12,6 +13,7 @@
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -114,6 +116,11 @@ public void onClick(View v) {
});

builder = new ArcadeBuilder(MainActivity.this);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
builder.setImageSize(preferences.getInt("preference_image_size", 128));
builder.setIterations(preferences.getInt("preference_iterations", 15));
builder.setContentWeight(preferences.getInt("preference_content_weight", 20));
builder.setStyleWeight(preferences.getInt("preference_style_weight", 200));


}
Expand All @@ -127,8 +134,13 @@ public boolean onCreateOptionsMenu(Menu menu) {
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
switch (id) {
case android.R.id.home:
onBackPressed();
break;
case R.id.action_settings:
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
break;
}

return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -174,6 +186,7 @@ public void onBackPressed() {
super.onBackPressed();
break;
case STATE_STYLE_CHOOSE:
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
currentState = STATE_CONTENT_CHOOSE;
content.setVisibility(View.VISIBLE);
showLogoView();
Expand Down Expand Up @@ -244,6 +257,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_STYLE_IMAGE:
currentState = STATE_BEGIN_STYLING;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
builder.setStyleimage(filePath);
ImageLoader.getInstance().displayImage(Uri.fromFile(new File(filePath)).toString(), styleImagePreview, options);
handler.postDelayed(new Runnable() {
Expand All @@ -260,6 +274,7 @@ public void run() {
case PICK_CONTENT_IMAGE:
builder.setContentImage(filePath);
content.setVisibility(View.GONE);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
currentState = STATE_STYLE_CHOOSE;
ImageLoader.getInstance().displayImage(Uri.fromFile(new File(filePath)).toString(), stylizedImage, options);
handler.postDelayed(new Runnable() {
Expand Down Expand Up @@ -404,7 +419,7 @@ protected String doInBackground(Void... params) {
if (!myDir.exists())
myDir.mkdirs();

File file = new File(myDir.getAbsolutePath(), name.replaceAll("\\s+","") + ".png");
File file = new File(myDir.getAbsolutePath(), name.replaceAll("\\s+", "") + ".png");
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
Expand Down
106 changes: 106 additions & 0 deletions app/src/main/java/com/naman14/arcade/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.naman14.arcade;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.MenuItem;
import android.widget.EditText;

/**
* Created by naman on 13/05/16.
*/
public class SettingsActivity extends AppCompatActivity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Settings");
getFragmentManager().beginTransaction().replace(R.id.container, new SettingsFragment()).commit();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home)
super.onBackPressed();
return super.onOptionsItemSelected(item);
}

public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {

SharedPreferences preferences;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);

preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
findPreference("preference_iterations").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showEditDialog(getActivity(), preferences, "Number of iterations", "Iterations", "preference_iterations");
return true;
}
});
findPreference("preference_style_weight").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showEditDialog(getActivity(), preferences, "Style weight", "Style weight", "preference_style_weight");
return true;
}
});
findPreference("preference_content_weight").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
showEditDialog(getActivity(), preferences, "Content weight", "Content weight", "preference_content_weight");
return true;
}
});
findPreference("preference_defaults").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
preferences.edit().putInt("preference_iterations", 15).apply();
preferences.edit().putInt("preference_style_weight", 200).apply();
preferences.edit().putInt("preference_content_weight", 20).apply();
preferences.edit().putInt("preference_image_size", 128).apply();
return true;
}
});
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
findPreference(key).setSummary(String.valueOf(preferences.getInt(key, -1)));
}
}

private static void showEditDialog(Context context, final SharedPreferences preferences, String title, String hint, final String key) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(title);
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_NUMBER_FLAG_SIGNED);
input.setHint(hint);
input.setText(String.valueOf(preferences.getInt(key, -1)));
alertDialog.setView(input);
alertDialog.setPositiveButton("Save",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
preferences.edit().putInt(key, Integer.parseInt(input.getText().toString())).apply();
}
});
alertDialog.show();
}
}
19 changes: 19 additions & 0 deletions app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:theme="@style/Theme.AppCompat" />

<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values-v21/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.FullScreen" parent="@style/AppTheme.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
15 changes: 15 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_image_size_entries">
<item>128</item>
<item>256</item>
<item>512</item>
</string-array>

<string-array name="pref_image_size_values">
<item>128</item>
<item>256</item>
<item>512</item>
</string-array>

</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

<style name="AppTheme.FullScreen" parent="@style/AppTheme.NoActionBar"></style>
</resources>
34 changes: 34 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory android:title="General">

<ListPreference
android:defaultValue="128"
android:entries="@array/pref_image_size_entries"
android:entryValues="@array/pref_image_size_values"
android:key="preference_image_size"
android:summary="The size of the output image to be generated"
android:title="Output Image Size" />

<Preference
android:defaultValue="15"
android:key="preference_iterations"
android:title="Number of iterations" />

<Preference
android:defaultValue="200"
android:key="preference_style_weight"
android:title="Style weight" />

<Preference
android:defaultValue="20"
android:key="preference_content_weight"
android:title="Content weight" />

<Preference
android:key="preference_defaults"
android:title="Reset to defaults" />

</PreferenceCategory>
</PreferenceScreen>
2 changes: 2 additions & 0 deletions arcade/arcade.iml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/bundles" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/classes" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/dependency-cache" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/appcompat-v7/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/exploded-aar/com.android.support/support-v4/23.1.1/jars" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/incremental" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
Expand Down
10 changes: 5 additions & 5 deletions arcade/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.naman14.arcade.library">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application android:allowBackup="true"
<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>
</application>
android:largeHeap="true"
android:supportsRtl="true"></application>

</manifest>
5 changes: 4 additions & 1 deletion arcade/src/main/assets/neural_style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,14 @@ function stylize(params)
local disp = deprocess(img:double())
disp = image.minmax { tensor = disp, min = 0, max = 1 }
local filename = build_filename(params.output_image, t)
local isFinal = false;
if t == params.num_iterations then
isFinal = true;
filename = params.output_image
end
updateProgress("Saving image")
image.save(filename, disp)
-- onImageSaved(filename, isFinal)
end
end

Expand Down Expand Up @@ -317,7 +320,7 @@ function stylize(params)
updateProgress('Running optimization with ADAM')
for t = 1, params.num_iterations do
updateProgress(string.format("Doing Iteration %i of %s ...", t, params.num_iterations))
-- updateIteration(t, params.num_iterations)
-- updateIteration(t, params.num_iterations)
local x, losses = optim.adam(feval, img, optim_state)
end
updateProgress("Done")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public ArcadeBuilder(Context context) {
this.styleLayers = "relu0,relu3,relu7,relu12";
this.protoFIle = "/storage/emulated/0/models/train_val.prototxt";
this.modelFile = "/storage/emulated/0/models/nin_imagenet_conv.caffemodel";
this.contentWeight = 100;
this.styleWeight = 100;
this.contentWeight = 20;
this.styleWeight = 200;
this.printIterations = 1;
this.saveIterations = 1;
this.styleScale = 1L;
this.pooling = "avg";
this.pooling = "max";
this.tvWeight = (long) 0.01;
this.seed = 123;
this.learningRate = 10;
Expand Down
Loading

0 comments on commit 0601176

Please sign in to comment.