冲天香阵透长安,满城尽带黄金甲。这篇文章主要讲述如何使用Android的Camera2 API相关的知识,希望能为你提供帮助。
[我正在尝试使用camera2 API创建可以拍照的应用程序,并将其保存到我的设备中以备后用。
我目前在拍照方面存在问题,预览效果很好,但是当我尝试拍照时却无法这样做。
package uk.ac.abertay.cmp309_courswork_1700673_keiranwait;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraMetadata;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Size;
import android.util.SparseIntArray;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CameraActivity extends AppCompatActivity {Button button;
TextureView textureView;
private static final SparseIntArray orientations = new SparseIntArray();
static {
orientations.append(Surface.ROTATION_0,90);
orientations.append(Surface.ROTATION_90,0);
orientations.append(Surface.ROTATION_180,270);
orientations.append(Surface.ROTATION_270,180);
}private String cameraId;
CameraDevice cameraDevice;
CameraCaptureSession cameraCaptureSession;
CaptureRequest captureRequest;
CaptureRequest.Builder captureRequestBuilder;
private Size imageDimensions;
private ImageReader imageReader;
private File file;
Handler mBackgroundHandler;
HandlerThread mBackgroundThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
textureView = findViewById(R.id.texture);
button = findViewById(R.id.btnPic);
textureView.setSurfaceTextureListener(textureListener);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
takePicture();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
});
}@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == 101){
if (grantResults[0] == PackageManager.PERMISSION_DENIED){
Toast.makeText(getApplicationContext(),"Sorry Camera Permission Denied",Toast.LENGTH_SHORT).show();
//finish();
}
}
}TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
try {
openCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}}@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {}@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {}
};
private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
cameraDevice = camera;
try {
createCameraPreview();
} catch (CameraAccessException e) {
e.printStackTrace();
}}@Override
public void onDisconnected(CameraDevice camera) {
cameraDevice.close();
}@Override
public void onError(CameraDevice camera, int error) {
cameraDevice.close();
cameraDevice = null;
}
};
private void createCameraPreview() throws CameraAccessException {
SurfaceTexture texture = textureView.getSurfaceTexture();
texture.setDefaultBufferSize(imageDimensions.getWidth(), imageDimensions.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(),"On Config",Toast.LENGTH_SHORT).show();
if (cameraDevice == null) {
return;
}cameraCaptureSession = session;
try {
updatePreview();
} catch (CameraAccessException e) {
e.printStackTrace();
}}@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(),"Configuration Changed",Toast.LENGTH_SHORT).show();
}
},null);
}private void updatePreview() throws CameraAccessException {
if (cameraDevice == null) {
return;
}captureRequestBuilder.set(captureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(),null, mBackgroundHandler);
}private void openCamera() throws CameraAccessException {
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
imageDimensions = map.getOutputSizes(SurfaceTexture.class) [0];
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED &
&
ActivityCompat.checkSelfPermission(this,Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(CameraActivity.this,new String[]{Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
return;
}manager.openCamera(cameraId,stateCallback,null);
}private void takePicture() throws CameraAccessException {
if (cameraDevice == null){
return;
}CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
Size[] jpegSizes = null;
jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
int width = 640;
int height = 480;
if (jpegSizes != null &
&
jpegSizes.length >
0){
width = jpegSizes[0].getWidth();
height = jpegSizes[0].getHeight();
}ImageReader reader = ImageReader.newInstance(width,height,ImageFormat.JPEG,1);
List<
Surface>
outputSurfaces = new ArrayList<
>
(2);
outputSurfaces.add(reader.getSurface());
outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(cameraDevice.TEMPLATE_STILL_CAPTURE);
captureBuilder.addTarget(reader.getSurface());
captureBuilder.set(CaptureRequest.CONTROL_MODE,CameraMetadata.CONTROL_MODE_AUTO);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(captureRequest.JPEG_ORIENTATION,orientations.get(rotation));
Long tsLong = System.currentTimeMillis() /1000;
String ts = tsLong.toString();
file = new File(Environment.getExternalStorageDirectory() + "/" + ts+".jpg");
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Toast.makeText(getApplicationContext(),"Image Avail",Toast.LENGTH_SHORT).show();
Image image = null;
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
try {
save(bytes);
} catch (IOException e) {
e.printStackTrace();
}finally {
if (image != null){
image.close();
}
}}
};
reader.setOnImageAvailableListener(readerListener,mBackgroundHandler);
final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureCompleted(CameraCaptureSession session,CaptureRequest request,TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show();
try {
createCameraPreview();
} catch (CameraAccessException e) {
e.printStackTrace();
}}
};
cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(),"On Config 2",Toast.LENGTH_SHORT).show();
try {
session.capture(captureBuilder.build(),captureListener,mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}}@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
}
},mBackgroundHandler);
}private void save(byte[] bytes) throws IOException {
OutputStream outputStream = null;
outputStream = new FileOutputStream(file);
outputStream.write(bytes);
outputStream.close();
}@Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (textureView.isAvailable()){
try {
openCamera();
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
else {
textureView.setSurfaceTextureListener(textureListener);
}}private void startBackgroundThread() {
mBackgroundThread = new HandlerThread("Camera Background");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}@Override
protected void onPause() {
try {
stopBackgroundThread();
} catch (InterruptedException e) {
e.printStackTrace();
}
super.onPause();
}protected void stopBackgroundThread() throws InterruptedException {
mBackgroundThread.quitSafely();
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
}
}
当它运行时,它似乎没有输入onImageAvailable方法,我相信这是问题所在,但是由于我对如何利用此API尚不了解,所以我不确定100%是否是问题所在。
答案【如何使用Android的Camera2 API】此问题已通过使用我的真实设备而不是模拟器来解决
推荐阅读
- Github页面和React App不管使用什么方法都无法工作
- 为什么Android测试运行器报告“空测试套件”()
- 如何在Android应用程序中启用TLS 1.2支持(在Android 4.1 JB上运行)
- Android游戏(Canvas还是OpenGL())
- “画布(Android Studio中试图绘制太大的位图”问题)
- 使用Google Apps脚本搜索无结果
- 试图将Google Pay集成到我的Android应用中
- 身份验证Apollo Graphql for android
- [android 8.0中的android套接字连接