- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试创建一个无需显示预览即可拍照的应用程序。使用本教程- https://www.youtube.com/watch?v=oPu42I0HSi4如果我使用 Activity,我设法让它工作。现在,我正尝试将处理拍照操作的代码移动到 IntentService 中。
这是我到目前为止得到的:
MainActivity.java - Activity
private void startCameraService()
{
if(ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this,new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},REQUEST_CAMERA_PERMISSION);
return;
}
Intent cameraService = new Intent(this, CameraService.class);
startService(cameraService);
}
CameraService.java -( Intent 服务)
public class CameraService extends IntentService {
//Input image state
private static final SparseIntArray ORIENTATIONS = new SparseIntArray();
static{
ORIENTATIONS.append(Surface.ROTATION_90,90);
ORIENTATIONS.append(Surface.ROTATION_0,0);
ORIENTATIONS.append(Surface.ROTATION_180,270);
ORIENTATIONS.append(Surface.ROTATION_270,180);
}
//Camera variables
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSessions;
private CaptureRequest.Builder captureRequestBuilder;
private Size imageDimension;
private ImageReader imageReader;
//Output file variables
private File file;
private static final int REQUEST_CAMERA_PERMISSION = 200;
private boolean mFlashSupported;
private Handler mBackgroundHandler;
private HandlerThread mBackgroundThread;
//the orientation of the device
int rotation;
//bitmap of the taken photo
private Bitmap bitmapImage;
//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onDisconnected(@NonNull CameraDevice cameraDevice) {
cameraDevice.close();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onError(@NonNull CameraDevice cameraDevice, int i) {
cameraDevice.close();
cameraDevice=null;
}
};
public CameraService() {
super("CameraService");
}
@Override
protected void onHandleIntent(Intent intent)
{
takePicture();
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void takePicture() {
Log.d("CameraService","inside TakePicture");
if(cameraDevice == null)
{
return;
}
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try{
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
if(characteristics != null)
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP)
.getOutputSizes(ImageFormat.JPEG);
//Capture image with custom size
int width = 640;
int height = 480;
if(jpegSizes != null && jpegSizes.length > 0)
{
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}
final ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<Surface> outputSurface = new ArrayList<>(2);
outputSurface.add(reader.getSurface());
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
File file = new File(Environment.getExternalStorageDirectory() + "/"+UUID.randomUUID().toString()+".jpg");
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = null;
try{
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
save(bytes);
bitmapImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
//TODO: fix the orientation so despite the phone position the photo is always correct and not rotated
// use ORIENTATIONS.get(rotation)
bitmapImage = rotateImage(bitmapImage, 270);
}
catch (Exeption e)
{
e.printStackTrace();
}
finally {
{
if(image != null)
image.close();
}
}
}
private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try{
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
}finally {
if(outputStream != null)
outputStream.close();
}
}
};
reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Log.d("CameraService","image is saved");
}
};
cameraDevice.createCaptureSession(outputSurface, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
try{
cameraCaptureSession.capture(captureBuilder.build(),captureListener,mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
}
},mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void openCamera() {
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
try{
cameraId = manager.getCameraIdList()[1];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
manager.openCamera(cameraId,stateCallback,null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
}
我的问题是,当我调用该服务时,cameraDevice 为 null。它应该在这里填充:
//camera state callback
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
}
但CameraDevice 相机 为空。
对于为什么这是作为一个Activity 而不是作为一个IntentService 有什么建议吗?如果你能帮助我,我将不胜感激
最佳答案
调用 onHandleIntent()
后,调用 stopSelf()
。因此,服务会在相机事件到来时停止。我猜你必须以某种方式让 Looper.loop()
在你的 onHandleIntent()
中运行。
关于java - 使用 IntentService 使用 Camera2 拍照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48768663/
所以我对 Cordova 还很陌生,现在我正在使用它来制作一个应用程序,我正在尝试拍照然后保存它。现在我还不能拍照。我所拥有的只是一个按钮,在相应的 javascript 文件中,按下按钮时会调用一个
我有完整的工作代码来拍照、裁剪、发送带有图像附件的电子邮件。 代码流程如下:拍张照片 > 找到并选择我们刚刚从图库中拍摄的照片> 裁剪它 > 通过电子邮件发送附件。 我想了解如何跳过整个“查找并选择我
我是编程新手,正在尝试使用板载摄像头硬件制作应用程序,我的目的是拍照;然后,当您单击“保存”时,该图片将出现在要编辑的新 Activity 中...我已经研究了几天如何最好地使用相机硬件...有人告诉
有没有一种方法可以使用 UIImagePickerController 中的叠加层来显示用户可能使用的方形图片,同时在某处有一个切换按钮可以即时切换? 目前 iOS 7 相机有这个功能,但是 UIIm
当我点击 GestureDetector 时,它会打开一个对话框,我可以在其中选择是要从图库中拍摄照片还是要自己制作照片 (the plugin)。但我的问题是当我点击对话框中的一个按钮时,对话框再次
我的一个页面上有一个 map 控件,我想拍一张照片并将其显示在不同的屏幕上。有谁知道我怎么能做到这一点? map 在 内. 最佳答案 您应该使用 WriteableBitmap 类的 Render
我一直在构建一个使用 Flurge 的 CameraKit 的项目,但我遇到了一个相当烦人的问题。其实我不知道怎么拍照。我有一个快门按钮,按下该按钮应该会激活 OnPicturetaken Liste
当我启动照片捕捉 Intent 时,返回给我的照片路径是:content://media/external/images/media/40209 但是当我查看我的设备时,照片路径应该类似于 [..]/
我结账Camera2Basic (谷歌样本) 我刚刚在类 Camera2BasicFragment 中更改了这个方法为了用前置摄像头拍照: private void setUpCameraOutp
在 fragment 中我想拍照但是我遇到了问题,我从来没有得到 onActivityResult 的回调 我的代码: private void dispatchTakePictureIntent(
当我使用 UIImagePicker 从相机拍照时,背景音乐(来自 ipod 应用程序、pandora 应用程序)停止。 Facebook 应用程序不会发生这种情况。有单独的代表来做这件事吗?请帮忙
我用这个方法拍了一张照片。 func convertImageFromCMSampleBufferRef(sampleBuffer:CMSampleBuffer) -> CIImage{ le
在我的 chrome 上,以下代码仅打开相机并立即崩溃,即显示黑屏。这段代码在 Firefox 上运行良好。为什么? http://jsfiddle.net/2mLb6cs2/ (function()
我正在尝试在我的 iOS 应用程序中实现 OpenCV。 CvVideoCamera 很棒,因为它有一个委托(delegate)方法,可以让我处理相机预览的每一帧,但我不知道如何提取一帧并将其保存为图
当我想使用相机拍照时,CameraSource 出现了一些问题。我想要的只是用我在 xml 文件上制作的按钮拍照并使用“CameraSource”拍照,因为我也在使用“TextRecognizer”。
可以禁用/删除此照片确认对话框: 我需要以某种方式跳过此对话框,但我仍想使用 Intent。我找到了这个 android: Take camera picture without "save" / "
我正在使用 FileProvider 在 Android Nougat 上拍照,这是我的代码 文件路径.xml: Java: String fileName =
将文件路径额外添加到图像捕获 Intent 会导致相机应用程序在 TF300t Android 平板电脑上出现故障,系统版本为 4.2.1。按“完成”按钮什么都不做——甚至不关闭相机应用程序 Acti
我正在构建一个将跟踪库存的网络内联网应用程序,我希望能够使用 iPad 的内置相机为每件元素拍照。这可能吗? 我已经使用应用对条形码做了类似的事情,但我还没有找到任何可以对照片做同样事情的东西。 最佳
我用这个方法拍了一张照片。 func convertImageFromCMSampleBufferRef(sampleBuffer:CMSampleBuffer) -> CIImage{ le
我是一名优秀的程序员,十分优秀!