我花了几天时间努力寻找从API camera2获取完整预览/照片的方法。当我从默认的相机应用程序或使用旧API的任何应用程序拍摄照片时,我得到了预览中的全部照片或拍摄照片。但我试过的所有使用camera2的应用程序都是切割左右边框
我尝试了很多东西来解决这种情况,例如将缩放设置为0,更改预览或捕获的大小,更改比例,模板,图片的格式但结果仍然被裁剪,我不知道为什么。我的代码有问题吗?这是API的特异性,并且对所有设备都一样吗?或者仅仅是我的平板电脑是否存在某种不兼容性?
这是我使用我的应用程序的预览:
cutted preview with camera2
以下是使用默认相机应用程序时的预览:
full preview with default application
这是我遇到问题的代码示例:
public class MainActivity extends AppCompatActivity
{
private static final int REQUEST_CAMERA_PERMISSION = 201;
private TextureView surfaceCamera;
private CameraDevice cameraDevice;
private Size imageDimension;
private CaptureRequest.Builder captureRequestBuilder;
private CameraCaptureSession cameraCaptureSessions;
CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback()
{
@Override
public void onOpened(@NonNull CameraDevice camera)
{
cameraDevice = camera;
createCameraPreview();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
cameraDevice.close();
}
}
@Override
public void onError(@NonNull CameraDevice camera, int error)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
cameraDevice.close();
}
cameraDevice = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceCamera = (TextureView) findViewById(R.id.surfaceCamera);
surfaceCamera.setSurfaceTextureListener(new TextureView.SurfaceTextureListener()
{
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1)
{
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture)
{
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture)
{
}
});
}
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
switch (requestCode)
{
case REQUEST_CAMERA_PERMISSION:
{
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
openCamera();//manager.openCamera(cameraId, stateCallback, null);
}
else
{
Toast.makeText(this, "Sorry!!!, you can't use this app without granting permission", Toast.LENGTH_LONG).show();
finish();
}
return;
}
default:
// other 'case' lines to check for other
// permissions this app might request
}
}
private void openCamera()
{
CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try
{
String cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
assert map != null;
imageDimension = map.getOutputSizes(SurfaceTexture.class)[0];
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
return;
}
manager.openCamera(cameraId, stateCallback, null);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
private void createCameraPreview()
{
try
{
SurfaceTexture texture = surfaceCamera.getSurfaceTexture();
assert texture != null;
texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
Surface surface = new Surface(texture);
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Collections.singletonList(surface), new CameraCaptureSession.StateCallback()
{
@Override
public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
//The camera is already closed
if (null == cameraDevice)
{
return;
}
// When the session is ready, we start displaying the preview.
cameraCaptureSessions = cameraCaptureSession;
updatePreview();
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession)
{
}
}, null);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
private void updatePreview()
{
try
{
cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, null);
}
catch (CameraAccessException e)
{
e.printStackTrace();
}
}
答案 0 :(得分:0)
我最终通过在平板电脑上安装最新版本的android来解决问题。它还修复了内存不足异常。
编辑: 我的不好,通过将android版本6.0升级到7.1,设备级别切换到遗留(它是有限的)。所以相机只使用旧的api throw camera2。有关信息,我正在使用星系标签A 6(2016)10'1。