我正在制作一个自定义android相机应用程序。到目前为止,我遇到的唯一问题是在横向模式下(将相机转90度)调整textureView
。观点扭曲而不是正确的方向。肖像模式正常工作。我不确定问题出在setupCamera()
方法,sensorToDeviceRotation()
方法或configureTransform()
方法中。我还没有实现任何实际拍摄照片。任何帮助表示赞赏。自定义相机textureView不适应风景模式
编辑/更新:我更新了我的代码以添加改进。尽管如此,原始问题仍然存在。我正在使用Pixel XL来测试此代码。我仍然不确定问题是什么。
homePage.java
public class homePage extends AppCompatActivity {
private static final int CAMERA_PERMISSION = 123;
private String mCameraId;
private Size mPreviewSize;
private CaptureRequest.Builder mCaptureRequestBuilder;
private HandlerThread mBackgroundHandlerThread;
private Handler mBackgroundHandler;
private static SparseIntArray ORIENTATIONS = new SparseIntArray();
static {
ORIENTATIONS.append(Surface.ROTATION_0, 0);
ORIENTATIONS.append(Surface.ROTATION_90, 90);
ORIENTATIONS.append(Surface.ROTATION_180, 180);
ORIENTATIONS.append(Surface.ROTATION_270, 270);
}
private CameraDevice mCameraDevice;
private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice camera) {
mCameraDevice = camera;
//Toast.makeText(getApplicationContext(), "Camera connected", Toast.LENGTH_SHORT).show();
startPreview();
}
@Override
public void onDisconnected(CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
};
private TextureView mTextureView;
private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
setupCamera(width, height);
connectCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
configureTransform(width, height);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
};
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
switch(requestCode) {
case CAMERA_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
return;
}else {
Toast.makeText(this, "Certain permissions needed to continue", Toast.LENGTH_SHORT).show();
}
break;
}
}
private boolean hasCamera() {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
return true;
}else {
AlertDialog camAlert = new AlertDialog.Builder(homePage.this).create();
camAlert.setTitle("Alert");
camAlert.setMessage("there is no usable camera");
camAlert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
camAlert.show();
return false;
}
}
private void setupCamera(int width, int height) {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);
if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_FRONT) {
continue;
}
StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
int totalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
boolean swapRotation = totalRotation == 90 || totalRotation == 270;
int rotatedWidth = width;
int rotatedHeight = height;
if(swapRotation) {
rotatedWidth = height;
rotatedHeight = width;
}
mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
mCameraId = cameraId;
return;
}
} catch (CameraAccessException e){
e.printStackTrace();
}
}
private void closeCamera() {
if(mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
}
private void connectCamera() {
CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED) {
cameraManager.openCamera(mCameraId, mCameraDeviceCallback, mBackgroundHandler);
//Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show();
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
//Toast.makeText(this, "non-marshmellow device permission granted", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION);
}
} else {
cameraManager.openCamera(mCameraId, mCameraDeviceCallback, mBackgroundHandler);
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void startPreview() {
SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface previewSurface = new Surface(surfaceTexture);
try {
mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
mCaptureRequestBuilder.addTarget(previewSurface);
mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(CameraCaptureSession session) {
try {
session.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(CameraCaptureSession session) {
Toast.makeText(getApplicationContext(), "Unable to set up camera preview", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void startBackgroundThread() {
mBackgroundHandlerThread = new HandlerThread("cameraString");
mBackgroundHandlerThread.start();
mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper());
}
private void stopBackgroundThread() {
mBackgroundHandlerThread.quitSafely();
try {
mBackgroundHandlerThread.join();
mBackgroundHandlerThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics,
int deviceOrientation) {
int sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
deviceOrientation = ORIENTATIONS.get(deviceOrientation);
return (sensorOrientation + deviceOrientation + 360) % 360;
}
private void configureTransform (int width, int height) {
if (null == mTextureView || null == mPreviewSize) {
return;
}
final int rotation = getWindowManager().getDefaultDisplay().getRotation();
final Matrix matrix = new Matrix();
final RectF viewRect = new RectF(0, 0, width, height);
final RectF buffetRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
final float centerX = viewRect.centerX();
final float centerY = viewRect.centerY();
if(Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
buffetRect.offset(centerX - buffetRect.centerX(), centerY - buffetRect.centerY());
matrix.setRectToRect(viewRect, buffetRect, Matrix.ScaleToFit.FILL);
final float scale = Math.max(
(float) height/mPreviewSize.getHeight(),
(float) width/mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
}else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
mTextureView.setTransform(matrix);
}
//=====added code from suggestions=======================
public interface CameraModule {
void onOrientationChanged(int orientation);
}
CameraModule mCurrentModule;
private MyOrientationEventListener mOrientationListener;
private int mLastRawOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;
private class MyOrientationEventListener extends OrientationEventListener {
public MyOrientationEventListener(Context context){
super(context);
}
@Override
public void onOrientationChanged(int orientation) {
if(orientation == ORIENTATION_UNKNOWN)
return;
mLastRawOrientation = orientation;
mCurrentModule.onOrientationChanged(orientation);
}
}
//========================================================================
private static class CompareSizeByArea implements Comparator<Size> {
@Override
public int compare(Size lhs, Size rhs) {
return Long.signum((long) lhs.getWidth() * lhs.getHeight()/
(long) rhs.getWidth() * rhs.getHeight());
}
}
private static Size chooseOptimalSize(Size[] choices, int width, int height) {
List<Size> bigEnough = new ArrayList<Size>();
for(Size option: choices) {
if (option.getHeight() == option.getWidth() * height/width &&
option.getWidth() >= width && option.getHeight() >= height) {
bigEnough.add(option);
}
}
if (bigEnough.size() > 0) {
return Collections.min(bigEnough, new CompareSizeByArea());
} else {
return choices[0];
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
hasCamera();
mTextureView = (TextureView) findViewById(R.id.textureView);
mOrientationListener = new MyOrientationEventListener(this);
}
@Override
protected void onResume() {
super.onResume();
startBackgroundThread();
if (mTextureView.isAvailable()) {
setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
connectCamera();
} else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
}
}
@Override
protected void onPause() {
closeCamera();
stopBackgroundThread();
super.onPause();
}
}
请注明您的具体问题? – Mandy8055
@ Mandy8055当我将手机转动90度以获取横向视图时,textureView无法正确调整。视图变得伸展并且以错误的方式旋转。 –
我不认为我正确实施您的建议。我的代码中有很多错误。 –