[英]Android Camera2 API preview sometimes distorted
我正在使用Camera2 API構建自定義相機。 到目前為止,除了有時會扭曲的預覽外,相機的效果非常好。 假設我連續7次打開相機。 所有嘗試都是成功的,第8次相機預覽失真。 看起來它使用寬度作為高度,反之亦然。
我的代碼基於camera2的Google示例實現,可以在這里找到。 有趣的是,即使是Google示例實現,有時也會出現這種扭曲的預覽。 我試圖修改AutoFitTextureView但沒有成功。 我目前正在使用Google再次提供的AutoFitTextureView.java。
在這里可以找到類似的帖子。 但是,提議的修復程序並沒有解決問題。
我可以通過更改setUpCameraOutputs方法中的以下內容來重現該問題:
mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
至:
mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
另一個奇怪的事情是,無論何時發生扭曲的預覽,你只需按下主頁按鈕,然后應用程序進入onPause()並再次打開應用程序,以便調用onResume(),每次預覽都是完美的。
有沒有人在這里遇到過這個問題並找到了解決方法?
提前致謝
我在Sony Xperia Z3 Tablet Compact上面臨同樣的問題。
Alex指出的拉取請求對我來說似乎不起作用。 它會導致相機預覽大於視圖區域(預覽被裁剪)。
雖然我無法專門跟蹤問題,但我找到了解決方法。 在打開相機的過程中,當mTextureView的大小發生變化時,似乎發生了失真。 延遲相機打開程序可以解決問題。
修改過的openCamera方法:
/**
* Opens the camera specified by {@link StepFragmentCamera#mCameraId}.
*/
private void openCamera(int width, int height) {
startBackgroundThread();
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
return;
}
setUpCameraOutputs(width, height);
configureTransform(width, height);
/*
* Delay the opening of the camera until (hopefully) layout has been fully settled.
* Otherwise there is a chance the preview will be distorted (tested on Sony Xperia Z3 Tablet Compact).
*/
mTextureView.post(new Runnable() {
@Override
public void run() {
/*
* Carry out the camera opening in a background thread so it does not cause lag
* to any potential running animation.
*/
mBackgroundHandler.post(new Runnable() {
@Override
public void run() {
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
});
}
});
}
最終修復了 Google Camera2Basic示例。 原始代碼有一個小的< vs >錯誤。 這是錯誤的2年。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.