Skip to content

Commit

Permalink
优化目录检查
Browse files Browse the repository at this point in the history
  • Loading branch information
tumuyan committed Apr 19, 2023
1 parent 4611a5d commit 94b4f10
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,31 +452,35 @@ private List<String> getExtraCommands(String extraPath, String extraCommand) {

if (!extraPath.isEmpty()) {
File[] folders = new File(extraPath).listFiles();
Arrays.sort(folders, (Comparator) Comparator.comparing(a -> ((File) a).getName()));
for (File folder : folders) {
String name = folder.getName();
if (folder.isDirectory() && name.startsWith("models")) {

// 匹配realsr模型
String model = name.replace("models-", "");
String scaleMatcher = ".*x(\\d+).*";
String noiseMatcher = "";
String command = "./realsr-ncnn -i input.png -o output.png -m " + folder.getAbsolutePath() + " -s ";

// 匹配waifu2x模型
if (name.matches("models-(cugan|cunet|upconv).*")) {
model = name.replace("models-", "Waifu2x-");
scaleMatcher = ".*scale(\\d+).*";
command = "./waifu2x-ncnn -i input.png -o output.png -m " + folder.getAbsolutePath() + " -s ";
noiseMatcher = "noise(\\d+).*";
} else if (name.startsWith("models-DF2K")) {
model = name.replace("models-", "RealSR-");
}
if (folders == null)
Log.e("getExtraCommands", "extraPath folders is null");
else {
Arrays.sort(folders, (Comparator) Comparator.comparing(a -> ((File) a).getName()));
for (File folder : folders) {
String name = folder.getName();
if (folder.isDirectory() && name.startsWith("models")) {

// 匹配realsr模型
String model = name.replace("models-", "");
String scaleMatcher = ".*x(\\d+).*";
String noiseMatcher = "";
String command = "./realsr-ncnn -i input.png -o output.png -m " + folder.getAbsolutePath() + " -s ";

// 匹配waifu2x模型
if (name.matches("models-(cugan|cunet|upconv).*")) {
model = name.replace("models-", "Waifu2x-");
scaleMatcher = ".*scale(\\d+).*";
command = "./waifu2x-ncnn -i input.png -o output.png -m " + folder.getAbsolutePath() + " -s ";
noiseMatcher = "noise(\\d+).*";
} else if (name.startsWith("models-DF2K")) {
model = name.replace("models-", "RealSR-");
}

List<String> suffix = genCmdFromModel(folder, scaleMatcher, noiseMatcher);
for (String s : suffix) {
cmdList.add(command + s);
cmdLabel.add(model + "-x" + s.replace(" -n ", "-noise"));
List<String> suffix = genCmdFromModel(folder, scaleMatcher, noiseMatcher);
for (String s : suffix) {
cmdList.add(command + s);
cmdLabel.add(model + "-x" + s.replace(" -n ", "-noise"));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
Expand Down Expand Up @@ -189,31 +188,13 @@ private void save() {
}

String extraPath = editExtraPath.getText().toString().trim();
if (!extraPath.isEmpty()) {
File file = new File(extraPath);
if (!file.exists()) {
editExtraPath.setError(getString(R.string.path_not_exist));
return;
}
if (!file.isDirectory()) {
editExtraPath.setError(getString(R.string.path_not_exist));
return;
}
}
if (folderHasErr(extraPath, editExtraPath))
return;
editor.putString("extraPath", extraPath);

String savePath = editSavePath.getText().toString().trim();
if (!savePath.isEmpty()) {
File file = new File(extraPath);
if (!file.exists()) {
editSavePath.setError(getString(R.string.path_not_exist));
return;
}
if (!file.isDirectory()) {
editSavePath.setError(getString(R.string.path_not_exist));
return;
}
}
if (folderHasErr(savePath, editSavePath))
return;
editor.putString("savePath", savePath);

editThread.setText(threadCount);
Expand All @@ -230,4 +211,20 @@ private void save() {

editor.apply();
}
}

public boolean folderHasErr(String path, EditText editText) {
if (!path.isEmpty()) {
File file = new File(path);
if (!file.exists()) {
editText.setError(getString(R.string.path_not_exist));
return true;
}
if (!file.isDirectory()) {
editText.setError(getString(R.string.path_not_dir));
return true;
}
}
return false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
<string name="preprocess_to_png">预处理webp/png/jpg/bmp以外的图片为png</string>
<string name="extra_path">自定义模型的路径(RealSR/ESRGAN/Waifu2x)</string>
<string name="path_not_exist">不存在此目录</string>
<string name="path_not_dir">此路径为一个文件,而不是目录</string>
<string name="menu_help">帮助</string>
<string name="menu_out">显示处理后的图片</string>
<string name="menu_in">显示处理前的图片</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ https://upscale.wiki/wiki/Model_Database There are many models here, download th
<string name="preprocess_to_png">Preprocess input image (not webp/png/jpg/bmp) to png</string>
<string name="extra_path">Path for custom models (RealSR/ESRGAN/Waifu2x)</string>
<string name="path_not_exist">Not exist this directory</string>
<string name="path_not_dir">The path is a file, not a directory</string>
<string name="menu_out2in" translatable="false"><![CDATA[Output -> Input]]></string>
<string name="menu_de_nearest">De Nearest</string>
<string name="menu_magick4">Magick 25%</string>
Expand Down

0 comments on commit 94b4f10

Please sign in to comment.