Решение для Camerax версии 1.0.0-alpha2
- Установить матрицу в преобразовании обновления.
- Повернуть текстовое поле для поворота предварительного просмотра
Аналогичный Logi c может быть применен и для Camera2 api (работает для меня)
private Preview setPreview() {
PreviewConfig pConfig = new PreviewConfig.Builder()
.setLensFacing(CameraX.LensFacing.BACK)
.build();
Preview preview = new Preview(pConfig);
preview.setOnPreviewOutputUpdateListener(
output -> {
ViewGroup parent = (ViewGroup) binding.viewFinder.getParent();
parent.removeView(binding.viewFinder);
parent.addView(binding.viewFinder, 0);
binding.viewFinder.setSurfaceTexture(output.getSurfaceTexture());
updateTransform();
});
return preview;
}
private void updateTransform() {
float viewWidth = binding.viewFinder.getMeasuredWidth();
float viewHeight = binding.viewFinder.getMeasuredHeight();
Size mPreviewSize = new Size(1080,720);
Activity activity = getActivity();
if (null == binding.viewFinder || null == mPreviewSize || null == activity) {
return;
}
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
float centerX = viewRect.centerX();
float centerY = viewRect.centerY();
if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
float scale = Math.max(
(float) viewHeight / mPreviewSize.getHeight(),
(float) viewWidth / mPreviewSize.getWidth());
matrix.postScale(scale, scale, centerX, centerY);
matrix.postRotate(90 * (rotation - 2), centerX, centerY);
} else if (Surface.ROTATION_180 == rotation) {
matrix.postRotate(180, centerX, centerY);
}
binding.viewFinder.setTransform(matrix);
}
int rotatiion = 0;
private void setRotationClickListener() {
binding.captureButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (rotatiion == 0) {
rotatiion = 90;
binding.viewFinder.setRotation(90);
}else if(rotatiion == 90) {
rotatiion = 180;
binding.viewFinder.setRotation(180);
}else if(rotatiion == 180) {
rotatiion = 270;
binding.viewFinder.setRotation(270);
}else if(rotatiion == 270) {
rotatiion = 0;
binding.viewFinder.setRotation(0);
}
}
});
}