Camera2 API问题 [英] Camera2 API Issue

查看:113
本文介绍了Camera2 API问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使一些相机应用程序使用camera2 APi示例camera2 api Google示例, 所以我的相机输出不是全屏

I make some camera App use camera2 APi example camera2 api google sample, so my camera output not full screen

然后我从camera2 api isue中读取内容,以解决我必须在AUtofitTextureView类的Onmeasure中更改此问题的情况:

then i read from camera2 api isue to solve that i have to change this in Onmeasure at AUtofitTextureView class:

if (width < height * mRatioWidth / mRatioHeight) {

来自<到> 就像这里:

from < to > so like here :

 if (width > height * mRatioWidth / mRatioHeight) {

然后将我的相机视图修复为全屏

then my camera view fix to fullscreen

但是现在我还有其他问题, 我的捕获图像比预览大.

but Now i have other issue, my capture image is bigger than my preview.

请从我的应用检查我的屏幕截图

please check my screen capture from my app

  1. 我的相机预览未全屏显示

  1. 如果相机预览未全屏显示,我会捕获图像

3.更改Meisure方法后,我的相机预览全屏显示

3. my camera preview Full screen after change on Meisure method

    在更改Meisure方法后,我可以从相机预览中捕获图像.
  1. my capture image from camera preview after change on Meisure method.

问题是我的捕获图像与相机预览不一样

the problem is that make my capture image not same with my camera preview

推荐答案

我已经尝试通过以下方式进行操作,这里共享了我的代码,用于保持16:9,18:9,19:9的宽高比.答案会有所帮助,基于设备分辨率选择ArrayList的最大分辨率到最小分辨率,这是我解决预览拉伸问题的方法.

I have tried by the following way, here is shared my code for maintaining the aspect ratio of 16:9,18:9,19:9.hope this answer will help, based on the device resolution choice ArrayList have max to min resolution this is the way I resolve the preview stretch issue.

//Samsung-S6-choices[0]
//Samsung-S7-edge-choices[6]
//OnePlus-5T-choices[15]
/*
 * Given {@code choices} of {@code Size}s supported by a camera, chooses the smallest one whose
 * width and height are at least as large as the respective requested values, and whose aspect
 * ratio matches with the specified value.
 *
 * @param choices     The list of sizes that the camera supports for the intended output class
 * @param width       The minimum desired width
 * @param height      The minimum desired height
 * @param aspectRatio The aspect ratio
 * @return The optimal {@code Size}, or an arbitrary one if none were big enough
 */
private  Size chooseOptimalSize(Size[] choices, int width, int height, Size aspectRatio) {
    // Collect the supported resolutions that are at least as big as the preview Surface
    List<Size> bigEnough = new ArrayList<>();
    int w = aspectRatio.getWidth();
    int h = aspectRatio.getHeight();
    double ratio = (double) h / w;
    int loopCounter=0;
    for (Size size : choices) {
        int orientation = getActivityContext.getResources().getConfiguration().orientation;
        if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=720) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=3840 ) {
            //if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=5120 ) {//Retina 5K
            if((size.getWidth()/16) == (size.getHeight()/9) && size.getWidth() <=7680 ) {//8K UHDTV Super Hi-Vision
                Log.e(TAG1, "chooseOptimalSize:"+size);
                return size;
            }
        } else {
            Log.e(TAG1, "chooseOptimalSize:--given--"+size);
            DisplayMetrics metrics = getActivityContext.getResources().getDisplayMetrics();
            float mCameraAspectRatio = ((float)metrics.heightPixels / (float)metrics.widthPixels);
            Log.e(TAG1, "chooseOptimalSize:--AspectRatio--"+mCameraAspectRatio);
            if((size.getWidth()/16) == (size.getHeight()/9) && ((size.getWidth() <=1280)||(size.getHeight()<=1920))) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=4320 ) ) {//8K UHDTV Super Hi-Vision
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=2880 ) ) {//Retina 5K
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=2160 ) ) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=1280 ) ) {
                //if((size.getWidth()/16) == (size.getHeight()/9) && (size.getWidth() <=4480 && size.getWidth() >=1280) ) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-16:9");
                return size;
            }else if((size.getWidth()/18) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-18:9");
                return size;
            }else if((size.getWidth()/18.5) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-18.5:9");
                return size;
            }else if((size.getWidth()/19) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-19:9");
                return size;
            }else if((size.getWidth()/19.5) == (size.getHeight()/9) && ((size.getWidth() <=3840)||(size.getHeight()<=2160))) {
                Log.e(TAG1, "chooseOptimalSize:"+size+"-19.5:9");
                return size;
            }else{
                Log.e(TAG1, "chooseOptimalSize"+" not proper aspect resolution");
                Log.e(TAG1, "chooseOptimalSize"+" ---=>Width---=>"+size.getWidth());
                Log.e(TAG1, "chooseOptimalSize"+" ---=>Height---=>"+size.getHeight());
            }
            //2340
        }

        if(screenWidth==size.getWidth()){
            Log.e(TAG1, loopCounter+".choose:width Matched:"+screenWidth+"="+size.getWidth());
        }else{
            Log.e(TAG1, loopCounter+".choose:width Not Matched:"+screenWidth+"="+size.getWidth());
        }

        if(screenHeight==size.getHeight()){
            Log.e(TAG1, loopCounter+".choose:height Matched:"+screenHeight+"="+size.getHeight());
        }else{
            Log.e(TAG1, loopCounter+".choose:height Not Matched:"+screenHeight+"="+size.getHeight());
        }
        loopCounter++;
    }
    // Pick the smallest of those, assuming we found any
    if (bigEnough.size() > 0) {
        return Collections.min(bigEnough, new CompareSizesByArea());
    } else {
        Log.e(TAG1, "Couldn't find any suitable preview size");
        return choices[0];
    }
}


/*
 * Compares two {@code Size}s based on their areas.
 */
static class CompareSizesByArea implements Comparator<Size> {
    @Override
    public int compare(Size lhs, Size rhs) {
        // We cast here to ensure the multiplications won't overflow
        return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
                (long) rhs.getWidth() * rhs.getHeight());
    }
}

希望有帮助,如有任何问题,请发表评论.

Hope it will help, please leave a comment, in case of any issue.

这篇关于Camera2 API问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆