Skip to content

Commit

Permalink
inttermediate images callback from lua
Browse files Browse the repository at this point in the history
  • Loading branch information
naman14 committed May 14, 2016
1 parent 451d57a commit 5b71be7
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 22 deletions.
12 changes: 7 additions & 5 deletions app/src/main/java/com/naman14/arcade/ArcadeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import com.naman14.arcade.library.Arcade;
import com.naman14.arcade.library.ArcadeBuilder;
import com.naman14.arcade.library.ArcadeUtils;
import com.naman14.arcade.library.listeners.CompletionListener;
import com.naman14.arcade.library.listeners.IterationListener;
import com.naman14.arcade.library.listeners.ImageSavedListener;
import com.naman14.arcade.library.listeners.ProgressListener;

/**
Expand All @@ -21,6 +20,7 @@ public class ArcadeService extends IntentService {

public static final String ACTION_START = "com.naman14.arcade.START";
public static final String ACTION_COMPLETED = "com.naman14.arcade.COMPLETE";
public static final String ACTION_IMAGE_SAVED = "com.naman14.arcade.IMAGE_SAVED";
public static final String ACTION_UPDATE_PROGRESS = "com.naman14.arcade.UPDATE_PROGRESS";

public static boolean isRunning;
Expand Down Expand Up @@ -63,10 +63,12 @@ public void onUpdateProgress(final String log, boolean important) {
currentLog = log;
}
});
arcade.setIterationListener(new IterationListener() {
arcade.setImageSavedListener(new ImageSavedListener() {
@Override
public void onIteration(int currentIteration, int totalIteration) {
Log.d("iterations", String.valueOf(currentIteration) + " of " + String.valueOf(totalIteration));
public void onImageSaved(String path) {
Intent localIntent = new Intent(ACTION_IMAGE_SAVED);
localIntent.putExtra("path", path);
LocalBroadcastManager.getInstance(ArcadeService.this).sendBroadcast(localIntent);
}
});
arcade.setCompletionListsner(new CompletionListener() {
Expand Down
20 changes: 16 additions & 4 deletions app/src/main/java/com/naman14/arcade/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.os.Handler;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
Expand Down Expand Up @@ -242,9 +243,13 @@ public void onClick(DialogInterface dialog, int which) {
break;
case STATE_STYLING_COMPLETED:
currentState = STATE_STYLE_CHOOSE;
animateViewVisiblity(styleImagePreview, false);
animateViewVisiblity(stylingResult, false);
animateViewVisiblity(start, false);
animateForegroundView(Color.parseColor("#88000000"), Color.parseColor("#44000000"));
if (logFragment != null) {
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().remove(manager.findFragmentById(R.id.log_container)).commit();
}
showStyleImages();
moveStyleButton(true);
break;
Expand Down Expand Up @@ -588,6 +593,8 @@ private void setupServiceReceiver() {
LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver, intentFilter);
IntentFilter intentFilter2 = new IntentFilter(ArcadeService.ACTION_COMPLETED);
LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver, intentFilter2);
IntentFilter intentFilter3 = new IntentFilter(ArcadeService.ACTION_IMAGE_SAVED);
LocalBroadcastManager.getInstance(this).registerReceiver(responseReceiver, intentFilter3);
}

private class ResponseReceiver extends BroadcastReceiver {
Expand All @@ -607,14 +614,19 @@ public void onReceive(Context context, Intent intent) {
stylingLog.setText(log);
}
break;
case ArcadeService.ACTION_IMAGE_SAVED:
String path = intent.getStringExtra("path");
DisplayImageOptions options1 = new DisplayImageOptions.Builder().displayer(new FadeInBitmapDisplayer(500)).build();
ImageLoader.getInstance().displayImage(Uri.fromFile(new File(path)).toString(), stylingResult, options1);
break;
case ArcadeService.ACTION_COMPLETED:
currentState = STATE_STYLING_COMPLETED;
serviceRunning = false;
hideLogoView(stylingView);
animateViewVisiblity(stylingResult, true);
String outputPath = Environment.getExternalStorageDirectory() + "/Arcade/outputs.output.png";
DisplayImageOptions options = new DisplayImageOptions.Builder().displayer(new FadeInBitmapDisplayer(500)).build();
ImageLoader.getInstance().displayImage(Uri.fromFile(new File(outputPath)).toString(), stylingResult, options);
String outputPath = Environment.getExternalStorageDirectory() + "/Arcade/outputs/output.png";
DisplayImageOptions options2 = new DisplayImageOptions.Builder().displayer(new FadeInBitmapDisplayer(500)).build();
ImageLoader.getInstance().displayImage(Uri.fromFile(new File(outputPath)).toString(), stylingResult, options2);
break;
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/com/naman14/arcade/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,16 @@ public void onCreate(Bundle savedInstanceState) {
@Override
public boolean onPreferenceClick(Preference preference) {
preferences.edit().putString("preference_iterations", "15").apply();
preferences.edit().putString("preference_save_iter", "5").apply();
preferences.edit().putString("preference_style_weight", "200").apply();
preferences.edit().putString("preference_content_weight", "20").apply();
preferences.edit().putString("preference_image_size", "128").apply();
preferences.edit().putString("preference_image_size", "256").apply();
preferences.edit().putBoolean("preference_logs", false).apply();
findPreference("preference_iterations").setSummary(preferences.getString("preference_iterations", "15"));
findPreference("preference_save_iter").setSummary(preferences.getString("preference_save_iter", "5"));
findPreference("preference_style_weight").setSummary(preferences.getString("preference_style_weight", "200"));
findPreference("preference_content_weight").setSummary(preferences.getString("preference_content_weight", "20"));
findPreference("preference_image_size").setSummary(preferences.getString("preference_image_size", "128"));
findPreference("preference_image_size").setSummary(preferences.getString("preference_image_size", "256"));
((SwitchPreference) findPreference("preference_logs")).setChecked(preferences.getBoolean("preference_logs", true));

return true;
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/res/layout/layout_styling.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<ImageView
android:id="@+id/stylingResultPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true" />

<LinearLayout
Expand All @@ -16,7 +16,7 @@
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="65dp"
android:layout_marginTop="85dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:visibility="gone">
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


<ListPreference
android:defaultValue="128"
android:defaultValue="256"
android:entries="@array/pref_image_size_entries"
android:entryValues="@array/pref_image_size_values"
android:key="preference_image_size"
Expand Down
2 changes: 1 addition & 1 deletion arcade/src/main/assets/neural_style.lua
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ function stylize(params)
updateProgress("Saving image")
image.save(filename, disp)
updateProgress(string.format("Image saved in %s ", filename))
-- onImageSaved(filename, isFinal)
onImageSaved(filename)
end
end

Expand Down
4 changes: 2 additions & 2 deletions arcade/src/main/java/com/naman14/arcade/library/Arcade.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ public static void onIterationUpdate(int current, int total) {
}

//Called from C
public static void onImageSaved(String path, boolean isFinal) {
public static void onImageSaved(String path) {
if (imageSavedListener != null) {
imageSavedListener.onImageSaved(path, isFinal);
imageSavedListener.onImageSaved(path);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
*/
public interface ImageSavedListener {

public void onImageSaved(String path, boolean isFinal);
public void onImageSaved(String path);
}
5 changes: 2 additions & 3 deletions arcade/src/main/jni/arcade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ static void onImageSaved(lua_State *L) {

size_t len;
const char *path = lua_tolstring(L, 1, &len);
bool isFinal = lua_toboolean(L, 2);

jclass clazz = globalEnv->FindClass("com/naman14/arcade/library/Arcade");
jmethodID onImageSaved = globalEnv->GetStaticMethodID(clazz, "onImageSaved",
"(Ljava/lang/String;Z)V");
"(Ljava/lang/String;)V");

jstring pathString = globalEnv->NewStringUTF(path);
globalEnv->CallStaticVoidMethod(clazz, onImageSaved, pathString, isFinal);
globalEnv->CallStaticVoidMethod(clazz, onImageSaved, pathString);
globalEnv->DeleteLocalRef(pathString);
globalEnv->DeleteLocalRef(clazz);

Expand Down

0 comments on commit 5b71be7

Please sign in to comment.