gpt4 book ai didi

android - 如何在没有任何用户交互的情况下使用 Intent 自动捕获图像

转载 作者:行者123 更新时间:2023-11-30 03:29:31 25 4
gpt4 key购买 nike

我正在尝试开发一个 android 应用程序,它可以使用相机 Intent 自动拍照而无需用户进行任何交互,但无法获取代码来自动触发图像捕获操作。有人帮忙吗??这是我的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pictureButton = findViewById(R.id.captureFront);
countDownTimer = new MyCountDownTimer(startTime, interval);

if (!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;

} else {
countDownTimer.cancel();
timerHasStarted = false;
// startB.setText("RESTART");
}

}

public void pictureCapture() throws IOException {
pictureButton.setEnabled(true);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File sampleDir = Environment.getExternalStorageDirectory();
try {
imagefile = File.createTempFile("image", ".jpeg", sampleDir);
} catch (IOException e) {
Log.e(TAG, "sdcard access error");
return;
}
takePicture(shutter, raw, postview, jpeg);

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

}

public final void takePicture(Camera.ShutterCallback shutter,
Camera.PictureCallback raw, Camera.PictureCallback postview,
Camera.PictureCallback jpeg) {

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" + data.getData(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
Toast.makeText(this, "Image cancelled", Toast.LENGTH_LONG)
.show();
} else {
// Image capture failed, advise user
Toast.makeText(this, "Image failed", Toast.LENGTH_LONG).show();
}
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}

@Override
public void onFinish() {
// text.setText("Time's up!, finishes");
countDownTimer.cancel();
try {
pictureCapture();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

@Override
public void onTick(long millisUntilFinished) {
// text.setText("" + millisUntilFinished / 1000);
}

}

};

最佳答案

是的,您可以使用 Camera Api 来完成。

Camera API in Android

无需执行任何 UI 来捕获图像,只需使用服务或任何后台线程或根据需要调用相机 api。

关于android - 如何在没有任何用户交互的情况下使用 Intent 自动捕获图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693230/

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