replace regex with sub string

This commit is contained in:
王劲鹏 2020-06-06 17:19:03 +08:00 committed by osborn
parent 46b789c2d1
commit 5e0b1ea92b
2 changed files with 38 additions and 19 deletions

View File

@ -29,6 +29,7 @@ import android.graphics.drawable.NinePatchDrawable;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.Pair;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -49,9 +50,6 @@ import com.github.pengfeizhou.jscore.JSONBuilder;
import com.github.pengfeizhou.jscore.JSObject; import com.github.pengfeizhou.jscore.JSObject;
import com.github.pengfeizhou.jscore.JSValue; import com.github.pengfeizhou.jscore.JSValue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jp.wasabeef.glide.transformations.BlurTransformation; import jp.wasabeef.glide.transformations.BlurTransformation;
import pub.doric.DoricContext; import pub.doric.DoricContext;
import pub.doric.extension.bridge.DoricPlugin; import pub.doric.extension.bridge.DoricPlugin;
@ -163,11 +161,11 @@ public class ImageNode extends ViewNode<ImageView> {
return new ColorDrawable(Color.GRAY); return new ColorDrawable(Color.GRAY);
} }
} else if (!TextUtils.isEmpty(placeHolderImageBase64)) { } else if (!TextUtils.isEmpty(placeHolderImageBase64)) {
Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); Pair<String, String> result = DoricUtils.translateBase64(placeHolderImageBase64);
Matcher m = r.matcher(placeHolderImageBase64); if (result != null) {
if (m.find()) { String imageType = result.first;
String imageType = m.group(1); String base64 = result.second;
String base64 = m.group(2);
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try { try {
byte[] data = Base64.decode(base64, Base64.DEFAULT); byte[] data = Base64.decode(base64, Base64.DEFAULT);
@ -177,6 +175,7 @@ public class ImageNode extends ViewNode<ImageView> {
} }
} }
} }
DoricLog.e("Cannot find PlaceHolderBase64 Drawable for " + placeHolderImageBase64); DoricLog.e("Cannot find PlaceHolderBase64 Drawable for " + placeHolderImageBase64);
return getDoricContext().getDriver().getRegistry().getDefaultPlaceHolderDrawable(); return getDoricContext().getDriver().getRegistry().getDefaultPlaceHolderDrawable();
} else if (placeHolderColor != Color.TRANSPARENT) { } else if (placeHolderColor != Color.TRANSPARENT) {
@ -199,11 +198,11 @@ public class ImageNode extends ViewNode<ImageView> {
return new ColorDrawable(Color.GRAY); return new ColorDrawable(Color.GRAY);
} }
} else if (!TextUtils.isEmpty(errorImageBase64)) { } else if (!TextUtils.isEmpty(errorImageBase64)) {
Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); Pair<String, String> result = DoricUtils.translateBase64(errorImageBase64);
Matcher m = r.matcher(errorImageBase64); if (result != null) {
if (m.find()) { String imageType = result.first;
String imageType = m.group(1); String base64 = result.second;
String base64 = m.group(2);
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try { try {
byte[] data = Base64.decode(base64, Base64.DEFAULT); byte[] data = Base64.decode(base64, Base64.DEFAULT);
@ -213,6 +212,7 @@ public class ImageNode extends ViewNode<ImageView> {
} }
} }
} }
DoricLog.e("Cannot find ErrorBase64 Drawable for " + errorImageBase64); DoricLog.e("Cannot find ErrorBase64 Drawable for " + errorImageBase64);
return getDoricContext().getDriver().getRegistry().getDefaultErrorDrawable(); return getDoricContext().getDriver().getRegistry().getDefaultErrorDrawable();
} else if (errorColor != Color.TRANSPARENT) { } else if (errorColor != Color.TRANSPARENT) {
@ -372,11 +372,12 @@ public class ImageNode extends ViewNode<ImageView> {
if (!prop.isString()) { if (!prop.isString()) {
return; return;
} }
Pattern r = Pattern.compile("data:image/(\\S+?);base64,(\\S+)"); String input = prop.asString().value();
Matcher m = r.matcher(prop.asString().value()); Pair<String, String> result = DoricUtils.translateBase64(input);
if (m.find()) { if (result != null) {
String imageType = m.group(1); String imageType = result.first;
String base64 = m.group(2); String base64 = result.second;
if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) { if (!TextUtils.isEmpty(imageType) && !TextUtils.isEmpty(base64)) {
try { try {
byte[] data = Base64.decode(base64, Base64.DEFAULT); byte[] data = Base64.decode(base64, Base64.DEFAULT);
@ -386,6 +387,7 @@ public class ImageNode extends ViewNode<ImageView> {
} }
} }
} }
break; break;
case "imagePath": case "imagePath":
if (!prop.isString()) { if (!prop.isString()) {

View File

@ -20,6 +20,7 @@ import android.content.res.AssetManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Rect; import android.graphics.Rect;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Pair;
import android.view.Display; import android.view.Display;
import android.view.WindowManager; import android.view.WindowManager;
@ -37,7 +38,6 @@ import org.json.JSONObject;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Array; import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@ -288,4 +288,21 @@ public class DoricUtils {
return byteBuffer.array(); return byteBuffer.array();
} }
public static Pair<String, String> translateBase64(String input) {
String typePrefix = "data:image/";
String dataPrefix = ";base64,";
int typeIndex = input.indexOf(typePrefix);
int dataIndex = input.indexOf(dataPrefix);
if (typeIndex != -1 && dataIndex != -1) {
String imageType = input.substring(typePrefix.length() + 1, dataIndex);
String base64 = input.substring(dataIndex + dataPrefix.length());
return new Pair<>(imageType, base64);
}
return null;
}
} }