Commit e08d779b by wanglei

更新读取Assets文件方法

1 parent 00e7d0f7
...@@ -19,18 +19,49 @@ import java.nio.MappedByteBuffer; ...@@ -19,18 +19,49 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel; import java.nio.channels.FileChannel;
public class Utils { public class Utils {
private static final String TAG = "Utils";
/** /**
* Memory-map the model file in Assets. * Memory-map the model file in Assets.
*/ */
public static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename) public static MappedByteBuffer loadModelFile(AssetManager assets, String filePath) throws IOException {
throws IOException { AssetFileDescriptor fileDescriptor = assets.openFd(filePath);
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename); MappedByteBuffer result = null;
try {
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor()); FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
try {
FileChannel fileChannel = inputStream.getChannel(); FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset(); long startOffset = fileDescriptor.getStartOffset();
long declaredLength = fileDescriptor.getDeclaredLength(); long declaredLength = fileDescriptor.getDeclaredLength();
return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength); result = fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
} catch (Throwable e) {
try {
inputStream.close();
} catch (Throwable e1) {
e.addSuppressed(e1);
}
throw e;
} finally {
inputStream.close();
}
} catch (Throwable e) {
if (fileDescriptor != null) {
try {
fileDescriptor.close();
} catch (Throwable e1) {
e.addSuppressed(e1);
}
}
Log.e(TAG, "WL_DEBUG loadMappedFile e = " + e, e);
} finally {
if (fileDescriptor != null) {
fileDescriptor.close();
}
}
return result;
} }
public static void softmax(final float[] vals) { public static void softmax(final float[] vals) {
...@@ -94,19 +125,14 @@ public class Utils { ...@@ -94,19 +125,14 @@ public class Utils {
* @param srcHeight Height of source frame. * @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame. * @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame. * @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to another. * @param applyRotation Amount of rotation to apply from one frame to
* Must be a multiple of 90. * another. Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant, * @param maintainAspectRatio If true, will ensure that scaling in x and y
* cropping the image if necessary. * remains constant, cropping the image if necessary.
* @return The transformation fulfilling the desired requirements. * @return The transformation fulfilling the desired requirements.
*/ */
public static Matrix getTransformationMatrix( public static Matrix getTransformationMatrix(final int srcWidth, final int srcHeight, final int dstWidth,
final int srcWidth, final int dstHeight, final int applyRotation, final boolean maintainAspectRatio) {
final int srcHeight,
final int dstWidth,
final int dstHeight,
final int applyRotation,
final boolean maintainAspectRatio) {
final Matrix matrix = new Matrix(); final Matrix matrix = new Matrix();
if (applyRotation != 0) { if (applyRotation != 0) {
...@@ -148,14 +174,14 @@ public class Utils { ...@@ -148,14 +174,14 @@ public class Utils {
return matrix; return matrix;
} }
public static Bitmap processBitmap(Bitmap source, int size){ public static Bitmap processBitmap(Bitmap source, int size) {
int image_height = source.getHeight(); int image_height = source.getHeight();
int image_width = source.getWidth(); int image_width = source.getWidth();
Bitmap croppedBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Bitmap croppedBitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Matrix frameToCropTransformations = getTransformationMatrix(image_width,image_height,size,size,0,false); Matrix frameToCropTransformations = getTransformationMatrix(image_width, image_height, size, size, 0, false);
Matrix cropToFrameTransformations = new Matrix(); Matrix cropToFrameTransformations = new Matrix();
frameToCropTransformations.invert(cropToFrameTransformations); frameToCropTransformations.invert(cropToFrameTransformations);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!