你好我这个应用程序保存图像每次我的脸移动类似于IG故事的东西我的问题是手机变得很慢而应用程序突然关闭因为分配内存问题我想知道如何我可以做到这一点,而不会减慢手机速度,也不会收到关闭错误。
下一个代码打开svg:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmap(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
Log.e("", "getBitmap: 1");
return bitmap;
}
private static Bitmap getBitmap(Context context, int drawableId) {
Log.e("", "getBitmap: 2");
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
return BitmapFactory.decodeResource(context.getResources(), drawableId);
} else if (drawable instanceof VectorDrawable) {
return getBitmap((VectorDrawable) drawable);
} else {
throw new IllegalArgumentException("unsupported drawable type");
}
}
下一个将svg画在脸部位置下方。
/**
* Draws the face annotations for position on the supplied canvas.
*/
@Override
public void draw(Canvas canvas) {
Face face = mFace;
if (face == null) {
return;
}
// Draws a circle at the position of the detected face, with the face's track id below.
float x = translateX(face.getPosition().x + face.getWidth() / 2);
float y = translateY(face.getPosition().y + face.getHeight() / 2);
canvas.drawCircle(x, y, FACE_POSITION_RADIUS, mFacePositionPaint);
canvas.drawText("id: " + mFaceId, x + ID_X_OFFSET, y + ID_Y_OFFSET, mIdPaint);
canvas.drawText("happiness: " + String.format("%.2f", face.getIsSmilingProbability()), x - ID_X_OFFSET, y - ID_Y_OFFSET, mIdPaint);
canvas.drawText("right eye: " + String.format("%.2f", face.getIsRightEyeOpenProbability()), x + ID_X_OFFSET * 2, y + ID_Y_OFFSET * 2, mIdPaint);
canvas.drawText("left eye: " + String.format("%.2f", face.getIsLeftEyeOpenProbability()), x - ID_X_OFFSET*2, y - ID_Y_OFFSET*2, mIdPaint);
// Draws a bounding box around the face.
float xOffset = scaleX(face.getWidth() / 2.0f);
float yOffset = scaleY(face.getHeight() / 2.0f);
float left = x - xOffset;
float top = y - yOffset;
float right = x + xOffset;
float bottom = y + yOffset;
//bitmap = BitmapFactory.decodeResource(getOverlay().getContext().getResources(), R.drawable.ic_shirt);
bitmap = getBitmap(getOverlay().getContext(), R.drawable.ic_tshirt);
float eyeX = left-400;
// for(Landmark l : face.getLandmarks()){
// if(l.getType() == Landmark.LEFT_EYE){
// eyeX = l.getPosition().x + bitmap.getWidth() / 2;
// }
// }
tshirt = Bitmap.createScaledBitmap(bitmap, (int) scaleX(bitmap.getWidth() / 2),
(int) scaleY(bitmap.getHeight()/2), false);
float top_shirt=(face.getPosition().y + face.getHeight())+200;
canvas.drawBitmap(tshirt, eyeX, top_shirt, new Paint());
Canvas myCanvas = new Canvas(tshirt);
myCanvas.drawBitmap(tshirt, eyeX, top_shirt, new Paint());
HashMap<String, Object> args = new HashMap<>();
args.put("tshirt", tshirt);
new saveImg(args).execute();
//canvas.drawRect(left, top, right, bottom, mBoxPaint);
}
这个将图像保存在手机上。
package com.google.android.gms.samples.vision.face.facetracker;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by diegohidalgo on 10/12/17.
*/
public class saveImg extends AsyncTask<Void, Void, Void> {
Bitmap tshirt;
String name;
saveImg(HashMap<String, Object> args){
tshirt = (Bitmap)args.get("tshirt");
File file=new File(Environment.getExternalStorageDirectory() + "/facedetection/");
File[] list = file.listFiles();
int count = 0;
for (File f: list){
String name = f.getName();
if (name.endsWith(".png"))
count++;
}
name="img"+count+".png";
}
@Override
protected Void doInBackground(Void... args) {
File file = new File(Environment.getExternalStorageDirectory() + "/facedetection/"+ name);
try {
tshirt.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
System.gc();
tshirt.recycle();
tshirt= null;
return null;
}
protected void onPostExecute() {
}
}
这是在关闭应用程序之前给我的错误。
10-13 10:38:17.526 8443-8443 / com.google.android.gms.samples.vision.face.facetracker W / art:抛出OutOfMemoryError&#34;无法分配8916492字节 分配1111888个空闲字节和1085KB直到OOM&#34; 10-13 10:38:18.020 8443-8443 / com.google.android.gms.samples.vision.face.facetracker I /处理:发送信号。 PID:8443 SIG:9
提前感谢。