gpt4 book ai didi

android - 带前置摄像头的NFC读卡器

转载 作者:行者123 更新时间:2023-11-30 01:48:19 25 4
gpt4 key购买 nike

我正在制作一个带有前置摄像头的应用程序,当用户点击他们的 NFC 卡时,它将捕获他们的图像。所以我为相机和 nfc 读卡器制作了一个 surfaceview,在一个 Activity 中添加了这两个功能。两者都是分开工作的,但是当我点击一张卡片时,surfaceview 变得毛躁,我没有得到任何返回。此行出错

NullPointerException

 mCamera.takePicture(null, null, ReadCardActivity.this);

我在 surfaceview 下方添加了一个按钮,如果我单击该图像,则可以毫无问题地捕获图像。

我的代码如下 -

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_read_card);
ButterKnife.bind(this);

mPreview = (SurfaceView) findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mDialog = new AlertDialog.Builder(this).setNeutralButton("Ok", null).create();

mAdapter = NfcAdapter.getDefaultAdapter(this);
if (mAdapter == null) {
showMessage(R.string.error, R.string.no_nfc);
finish();
return;
}

// do we have a camera?
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG).show();
finish();
} else {
cameraId = findFrontFacingCamera();
//cameraId = findBackFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "Sorry you don't have secondary camera", Toast.LENGTH_LONG).show();
finish();
} else {
releaseCameraAndPreview();
mCamera = Camera.open(cameraId);
}
}

mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
mAdapter = NfcAdapter.getDefaultAdapter(this);

mNdefPushMessage = new NdefMessage(new NdefRecord[]{newTextRecord(
"Message from NFC Reader :-)", Locale.ENGLISH, true)});
}


private void releaseCameraAndPreview() {
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}

@Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
mAdapter.disableForegroundDispatch(this);
Log.d("CAMERA", "Destroy");
}

@OnClick(R.id.buttonCaptureImage)
public void capturingImage() {
System.gc();
mPreview.post(new Runnable() {
public void run() {
mCamera.takePicture(null, null, ReadCardActivity.this);
}
});
}

最佳答案

我认为您需要自己的回调而不是 ReadCardActivity.this

我处理与考勤相同的事情(当用户伴随着标签,然后前置摄像头拍照),所以我可以提供我的代码(但在 C# Xamarin 中)

这是回调类

    private class PictureTakenCallback : Java.Lang.Object, Camera.IPictureCallback
{
private Context parentContext;
public event PictureTakenEventHandler OnPictureTakenAndReady;
public PictureTakenCallback()
{//There have to empty construtor
}
public PictureTakenCallback(Context pParentContext)
{
this.parentContext = pParentContext;
}
public void OnPictureTaken(byte[] pData, Camera pCamera)
{
try
{
if (OnPictureTakenAndReady != null) OnPictureTakenAndReady(null, new PictureTakenEventArgs(pData));
}
catch (Exception ex)
{
string msg = ex.Message;
((Activity)parentContext).RunOnUiThread(() => { Toast.MakeText(parentContext, "PictureTakenCallback Exception", ToastLength.Long).Show(); });
}
}
}

相机打开后创建回调

          pictureTakenCallback = new PictureTakenCallback(parentContext);
pictureTakenCallback.OnPictureTakenAndReady += new PictureTakenEventHandler(pictureTakenCallback_OnPictureTakenAndReady);

当你接受来自nfcAdapter的 Intent 时调用这个方法

 public void TakePicture()
{
if (cameraFront != null)
{
try
{
cameraFront.TakePicture(null, null, null, pictureTakenCallback);
((Activity)parentContext).RunOnUiThread(() => { Toast.MakeText(parentContext, "After TakePicture", ToastLength.Long).Show(); });
}
catch (Exception ex)
{
((Activity)parentContext).RunOnUiThread(() => { Toast.MakeText(parentContext, "Exception during TakePicture", ToastLength.Long).Show(); }); string msg = ex.Message;
}
}
}

如果相机拍对了,就来参加这个 Activity

private void pictureTakenCallback_OnPictureTakenAndReady(object sender, PictureTakenEventArgs e)
{
if (e.Data != null && e.Data.Length > 0) byte[] photoInBytes = e.Data;
if (cameraFront != null)
{
cameraFront.StartPreview();
CameraController.IsPreviewStopped = true;
}
}

注意,你需要StartPreview权限,SurfaceView必须可见

就我个人而言,我建议将相机的所有内容都放入单独的文件/类中,并制作一个简单的界面,而不是直接将其提供给 Activity 。

关于android - 带前置摄像头的NFC读卡器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33409621/

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