gpt4 book ai didi

android - 表面支架上的凸轮图片,绘制 : Exception because of surface type

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:02 25 4
gpt4 key购买 nike

我使用带有 SurfaceHolderSurfaceView 在我的测试应用程序中开始相机预览。

public class TextLocatorActivity extends Activity {

private Preview pvw;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

pvw = new Preview(this);
setContentView(pvw);
}

我想使用 CameraPreview(随 SDK 版本 7 的 SDK 示例一起提供)。单击 UI 即可拍照。这是 Preview 类:

public class Preview extends SurfaceView implements OnClickListener, SurfaceHolder.Callback {

SurfaceHolder holder;
Camera cam;

final static String TAG = "TextLocator:Preview";

Preview(Context context) {
super(context);

holder = getHolder();
holder.addCallback(this);
this.setOnClickListener(this);
// seems to be required (although the docs state, this enum is deprecated, as the type will be chosen automatically...
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
cam = Camera.open();
try {
Camera.Parameters params = cam.getParameters();
params.set("orientation", "landscape");
Camera.Size bestSize = getBestPreviewSize(480, 320);
params.setPreviewSize(bestSize.width, bestSize.height);
cam.setParameters(params);
// where to draw:
cam.setPreviewDisplay(holder);
} catch (IOException exception) {
cam.release();
cam = null;
// TODO: add more exception handling logic here
}
}

private Camera.Size getBestPreviewSize(int width, int height)
{
// checks for supported sizes close to the demanded values - implemented.
}


public void surfaceDestroyed(SurfaceHolder holder) {
cam.stopPreview();
cam.release();
cam = null;
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
Camera.Parameters parameters = cam.getParameters();
parameters.setPreviewSize(w, h);

cam.setParameters(parameters);
cam.startPreview();
}

Camera.PictureCallback picCallback = new Camera.PictureCallback() {

public void onPictureTaken(byte[] bytes, Camera arg1) {
synchronized(holder) {
Canvas c = null;

try {
c = holder.lockCanvas();

Paint paint = new Paint();
paint.setAntiAlias(false);
paint.setARGB(200, 120, 180, 0);

c.drawLine(10, 10, c.getWidth() - 10, c.getHeight() - 10, paint);
}
catch (Exception e) {
Log.d(TAG, "Exception: " + e.getMessage());
// IllegalArguementException Surface Type is SURFACE_TYPE_PUSH_BUFFERS
}
finally {
holder.unlockCanvasAndPost(c);
}
}
}
};

public void onClick(View v)
{
cam.takePicture(null, null, picCallback);
}

接下来,我尝试对 SurfaceHolder 的相应 Canvas 进行一些额外的绘制。因此,我正在调用 canvas = holder.lockCanvas()。这将导致 IllegalArgumentException 消息:

Surface type is SURFACE_TYPE_PUSH_BUFFERS

现在,docs声明那些表面类型已被弃用,值集将被忽略。但是,只有当我将类型设置为此特定值时,相机预览才有效。

如何在显示拍摄图片的SurfaceViewCanvas上实现绘图?或者应该放在不同的层/ View 上?

最佳答案

您不能既显示相机预览又使用 Canvas 绘图。你必须做其中之一。

关于android - 表面支架上的凸轮图片,绘制 : Exception because of surface type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6336602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com