feat:android support ArrayBuffer Resource
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright [2021] [Doric.Pub]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package pub.doric.plugin;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSONBuilder;
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
import com.github.pengfeizhou.jscore.JavaValue;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.async.AsyncResult;
|
||||
import pub.doric.extension.bridge.DoricMethod;
|
||||
import pub.doric.extension.bridge.DoricPlugin;
|
||||
import pub.doric.extension.bridge.DoricPromise;
|
||||
import pub.doric.resource.DoricResource;
|
||||
import pub.doric.utils.DoricLog;
|
||||
|
||||
/**
|
||||
* @Description: This is for loading resource into js as ArrayBuffer
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2021/11/18
|
||||
*/
|
||||
@DoricPlugin(name = "imageDecoder")
|
||||
public class ImageDecoderPlugin extends DoricJavaPlugin {
|
||||
|
||||
public ImageDecoderPlugin(DoricContext doricContext) {
|
||||
super(doricContext);
|
||||
}
|
||||
|
||||
@DoricMethod
|
||||
public void getImageInfo(final JSObject resource, final DoricPromise promise) {
|
||||
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
|
||||
getDoricContext(),
|
||||
resource);
|
||||
if (doricResource != null) {
|
||||
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
|
||||
@Override
|
||||
public void onResult(byte[] rawData) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inJustDecodeBounds = true;
|
||||
BitmapFactory.decodeByteArray(rawData, 0, rawData.length, options);
|
||||
promise.resolve(new JavaValue(new JSONBuilder()
|
||||
.put("width", options.outWidth)
|
||||
.put("height", options.outHeight)
|
||||
.put("mimeType", options.outMimeType)
|
||||
.toJSONObject()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
|
||||
}
|
||||
});
|
||||
} else {
|
||||
DoricLog.e("Cannot find loader for resource %s", resource);
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
}
|
||||
|
||||
@DoricMethod
|
||||
public void decodeToPixels(final JSObject resource, final DoricPromise promise) {
|
||||
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
|
||||
getDoricContext(),
|
||||
resource);
|
||||
if (doricResource != null) {
|
||||
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
|
||||
@Override
|
||||
public void onResult(byte[] rawData) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
|
||||
Bitmap bitmap = BitmapFactory.decodeByteArray(rawData, 0, rawData.length);
|
||||
ByteBuffer buffer = ByteBuffer.allocate(bitmap.getByteCount());
|
||||
bitmap.copyPixelsToBuffer(buffer);
|
||||
promise.resolve(new JavaValue(buffer.array()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFinish() {
|
||||
|
||||
}
|
||||
});
|
||||
} else {
|
||||
DoricLog.e("Cannot find loader for resource %s", resource);
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -43,15 +43,9 @@ public class ResourceLoaderPlugin extends DoricJavaPlugin {
|
||||
}
|
||||
|
||||
@DoricMethod
|
||||
public void load(JSObject resource, final DoricPromise promise) {
|
||||
final String resId = resource.getProperty("resId").asString().value();
|
||||
final String type = resource.getProperty("type").asString().value();
|
||||
final String identifier = resource.getProperty("identifier").asString().value();
|
||||
public void load(final JSObject resource, final DoricPromise promise) {
|
||||
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager().load(
|
||||
getDoricContext(),
|
||||
resId,
|
||||
type,
|
||||
identifier);
|
||||
getDoricContext(), resource);
|
||||
if (doricResource != null) {
|
||||
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
|
||||
@Override
|
||||
@@ -62,7 +56,7 @@ public class ResourceLoaderPlugin extends DoricJavaPlugin {
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
DoricLog.e("Cannot load resource type = %s, identifier = %s, %s", type, identifier, t.getLocalizedMessage());
|
||||
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
|
||||
@@ -72,7 +66,7 @@ public class ResourceLoaderPlugin extends DoricJavaPlugin {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
DoricLog.e("Cannot find loader for resource type = %s, identifier = %s", type, identifier);
|
||||
DoricLog.e("Cannot find loader for resource %s", resource);
|
||||
promise.reject(new JavaValue("Load error"));
|
||||
}
|
||||
}
|
||||
|
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright [2021] [Doric.Pub]
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package pub.doric.resource;
|
||||
|
||||
import pub.doric.DoricContext;
|
||||
import pub.doric.async.AsyncResult;
|
||||
|
||||
/**
|
||||
* @Description: This represents an arrayBuffer resource
|
||||
* @Author: pengfei.zhou
|
||||
* @CreateDate: 2021/11/18
|
||||
*/
|
||||
public class DoricArrayBufferResource extends DoricResource {
|
||||
private final byte[] data;
|
||||
|
||||
public DoricArrayBufferResource(DoricContext doricContext, byte[] data) {
|
||||
super(doricContext, "");
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AsyncResult<byte[]> fetchRaw() {
|
||||
return new AsyncResult<>(data);
|
||||
}
|
||||
}
|
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package pub.doric.resource;
|
||||
|
||||
import com.github.pengfeizhou.jscore.JSObject;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
@@ -43,17 +45,18 @@ public class DoricResourceManager {
|
||||
|
||||
@Nullable
|
||||
public synchronized DoricResource load(@NonNull DoricContext doricContext,
|
||||
@NonNull String resId,
|
||||
@NonNull String type,
|
||||
@NonNull String identifier) {
|
||||
DoricResource resource = cachedResources.get(resId);
|
||||
if (resource == null) {
|
||||
@NonNull JSObject resource) {
|
||||
String resId = resource.getProperty("resId").asString().value();
|
||||
String type = resource.getProperty("type").asString().value();
|
||||
String identifier = resource.getProperty("identifier").asString().value();
|
||||
DoricResource doricResource = cachedResources.get(resId);
|
||||
if (doricResource == null) {
|
||||
DoricResourceLoader loader = mResourceLoaders.get(type);
|
||||
if (loader != null) {
|
||||
resource = loader.load(doricContext, identifier);
|
||||
cachedResources.put(resId, resource);
|
||||
doricResource = loader.load(doricContext, identifier);
|
||||
cachedResources.put(resId, doricResource);
|
||||
}
|
||||
}
|
||||
return resource;
|
||||
return doricResource;
|
||||
}
|
||||
}
|
||||
|
@@ -396,12 +396,9 @@ public class ImageNode extends ViewNode<ImageView> {
|
||||
if (!prop.isObject()) {
|
||||
return;
|
||||
}
|
||||
JSObject resource = prop.asObject();
|
||||
final String resourceId = resource.getProperty("resId").asString().value();
|
||||
final String type = resource.getProperty("type").asString().value();
|
||||
final String identifier = resource.getProperty("identifier").asString().value();
|
||||
final JSObject resource = prop.asObject();
|
||||
DoricResource doricResource = getDoricContext().getDriver().getRegistry().getResourceManager()
|
||||
.load(getDoricContext(), resourceId, type, identifier);
|
||||
.load(getDoricContext(), resource);
|
||||
if (doricResource != null) {
|
||||
doricResource.fetch().setCallback(new AsyncResult.Callback<byte[]>() {
|
||||
@Override
|
||||
@@ -412,7 +409,7 @@ public class ImageNode extends ViewNode<ImageView> {
|
||||
@Override
|
||||
public void onError(Throwable t) {
|
||||
t.printStackTrace();
|
||||
DoricLog.e("Cannot load resource type = %s, identifier = %s, %s", type, identifier, t.getLocalizedMessage());
|
||||
DoricLog.e("Cannot load resource %s, %s", resource.toString(), t.getLocalizedMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -421,7 +418,7 @@ public class ImageNode extends ViewNode<ImageView> {
|
||||
}
|
||||
});
|
||||
} else {
|
||||
DoricLog.e("Cannot find loader for resource type = %s, identifier = %s", type, identifier);
|
||||
DoricLog.e("Cannot find loader for resource %s", resource.toString());
|
||||
}
|
||||
break;
|
||||
case "imageUrl":
|
||||
|
Reference in New Issue
Block a user