我正在使用从处理我的视频输入的 TextureView 获得的位图图像进行一些图像处理测试。在我的测试中,我注意到 TextureView.getBitmap(Bitmap) 运行所需的时间,有时会因执行而异。
为了演示这个问题,我修改了 TextureView API参考中的一个示例。在下面的代码中,我使用相机作为 TextureView 的输入,抓取位图并将其渲染到 SurfaceView,测量需要多少时间。我已经在运行 Android 4.2.2 的一对 Nexus 7 设备上对其进行了测试。我实际上注意到了两种模式,一种是慢的,一种是快的。
当执行“慢”时,getBitmap(Bitmap) 大约需要 20 毫秒。当执行“快速”时,getBitmap(Bitmap) 大约需要 15 毫秒。
大多数情况下,该应用程序以“慢”模式运行。如果我摆弄设备或重新编译/重新安装应用程序,有时我会让它在“快速”模式下工作。但之后它会再次缓慢运行。速度仅在程序重新启动后发生变化,在执行期间不会发生变化。
顺便说一句,将位图转储到 SurfaceView 只需要大约 8 毫秒:我想知道为什么从 TextureView 中取出位图需要两倍以上的时间。
问题:
- 为什么会发生这种执行时间的差异?
- 有没有办法确保 getBitmap(Bitmap) 总是尽可能快地运行?
- 写入 SurfaceView 比读取 TextureView 快两倍是否合理?
谢谢你。
——拉斐尔
PS:我已经在 Android Developers Google Group 上发布了这个,但它似乎自上周以来一直处于适度状态。抱歉交叉发帖。
编辑:
我在 GitHub 上添加了一个示例项目,供那些希望更轻松地尝试它的人使用:
活动:
import java.io.IOException;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
private Camera mCamera;
private TextureView mTextureView;
private SurfaceView mSurfaceView;
private final int imgW = 640;
private final int imgH = 480;
private Bitmap bmp = Bitmap.createBitmap(imgW, imgH, Bitmap.Config.ARGB_8888);
private Canvas canvas = new Canvas(bmp);
private Paint paint1 = new Paint();
private SurfaceHolder mSurfaceHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mTextureView = (TextureView) findViewById(R.id.textureview);
mTextureView.setSurfaceTextureListener(this);
final int textSize = 24;
paint1.setColor(0xff00ffff);
paint1.setTextSize(textSize);
}
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCamera = Camera.open(Camera.getNumberOfCameras()-1);
Camera.Parameters parameters = mCamera.getParameters();
parameters.setPreviewSize(imgW, imgH);
mCamera.setParameters(parameters);
try {
mCamera.setPreviewTexture(surface);
mCamera.startPreview();
} catch (IOException ioe) {
// Something bad happened
}
}
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
// Ignored, Camera does all the work for us
}
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCamera.stopPreview();
mCamera.release();
return true;
}
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// Invoked every time there's a new Camera preview frame
long time0 = System.currentTimeMillis();
mTextureView.getBitmap(bmp);
long time1 = System.currentTimeMillis() - time0;
final Canvas c = mSurfaceHolder.lockCanvas();
if ( c != null) {
canvas.drawText("getBmp= " + time1, 10, 40, paint1);
c.drawBitmap(bmp, 0, 0, null);
mSurfaceHolder.unlockCanvasAndPost(c);
}
long total = System.currentTimeMillis() - time0;
long time2 = total -time1;
Log.i("onFrame", "timing: getBmp= " + time1 + " blit= " + time2 + " total= " + total);
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="640px"
android:layout_height="480px" />
<TextureView
android:id="@+id/textureview"
android:layout_width="640px"
android:layout_height="480px" />
</LinearLayout>