Commit e08d779b by wanglei

更新读取Assets文件方法

1 parent 00e7d0f7
......@@ -19,18 +19,49 @@ import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class Utils {
private static final String TAG = "Utils";
/**
* Memory-map the model file in Assets.
*/
public static MappedByteBuffer loadModelFile(AssetManager assets, String modelFilename)
throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(modelFilename);
public static MappedByteBuffer loadModelFile(AssetManager assets, String filePath) throws IOException {
AssetFileDescriptor fileDescriptor = assets.openFd(filePath);
MappedByteBuffer result = null;
try {
FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
try {
FileChannel fileChannel = inputStream.getChannel();
long startOffset = fileDescriptor.getStartOffset();
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) {
......@@ -94,19 +125,14 @@ public class Utils {
* @param srcHeight Height of source frame.
* @param dstWidth Width of destination frame.
* @param dstHeight Height of destination frame.
* @param applyRotation Amount of rotation to apply from one frame to another.
* Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y remains constant,
* cropping the image if necessary.
* @param applyRotation Amount of rotation to apply from one frame to
* another. Must be a multiple of 90.
* @param maintainAspectRatio If true, will ensure that scaling in x and y
* remains constant, cropping the image if necessary.
* @return The transformation fulfilling the desired requirements.
*/
public static Matrix getTransformationMatrix(
final int srcWidth,
final int srcHeight,
final int dstWidth,
final int dstHeight,
final int applyRotation,
final boolean maintainAspectRatio) {
public static Matrix getTransformationMatrix(final int srcWidth, final int srcHeight, final int dstWidth,
final int dstHeight, final int applyRotation, final boolean maintainAspectRatio) {
final Matrix matrix = new Matrix();
if (applyRotation != 0) {
......@@ -148,14 +174,14 @@ public class Utils {
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_width = source.getWidth();
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();
frameToCropTransformations.invert(cropToFrameTransformations);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!