简体   繁体   中英

Android: reduce height of TextureView for camera preview

I'm studing this Google project with an example of Camera2 using: https://github.com/googlesamples/android-Camera2Basic

This example works perfectly. Now I want to have the area preview smaller than in this example; so I changed the ratio like this:

mTextureView.setAspectRatio(getScreenWidth(), getScreenHeight() - bottom_area_height);

but the result is bad... the preview doesn't have a good ratio now (is too stretched horizontally): 在此处输入图片说明

Have you got an idea guys?

Thank you very much!

You did get what you asked for, which is a different aspect ratio. Unfortunately, there's only one aspect ratio that's correct, since it needs to match that of the camera preview buffers.

If you want a smaller preview, you need to shrink it in both directions to maintain the correct aspect ratio. So if you update the layout to have a narrower space, and keep the aspect ratio the same, the height should also go down. Or reduce the height of the surrounding frame; in any case, the aspect ratio must remain what it is to have no stretching.

AutoFitTextureView isn't designed to crop the preview, which is the other option for avoiding stretching. That could be done by messing with the TextureView transform, but it does mean you're cutting off some of the view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM