久久国产成人av_抖音国产毛片_a片网站免费观看_A片无码播放手机在线观看,色五月在线观看,亚洲精品m在线观看,女人自慰的免费网址,悠悠在线观看精品视频,一级日本片免费的,亚洲精品久,国产精品成人久久久久久久

分享

android camera中的預(yù)覽圖片變形的解決辦法

 深呼吸_DA 2014-08-25

摘要 之前處理相機(jī)模塊的時(shí)候,,遇到了一個(gè)問題,就是調(diào)用系統(tǒng)的camera的時(shí)候在拍照預(yù)覽界面的時(shí)候看到的照片是變形的,。不曉得大家有沒有和我遇到一樣的問題,,后來(lái)發(fā)現(xiàn)是我的布局界面沒有按對(duì)應(yīng)的比例進(jìn)行縮放導(dǎo)致的,。后來(lái)參考了系統(tǒng)camera中的代碼解決了這個(gè)問題

之前處理相機(jī)模塊的時(shí)候,遇到了一個(gè)問題,,就是調(diào)用系統(tǒng)的camera的時(shí)候在拍照預(yù)覽界面的時(shí)候看到的照片是變形的,。不曉得大家有沒有和我遇到一樣的問題,后來(lái)發(fā)現(xiàn)是我的布局界面沒有按對(duì)應(yīng)的比例進(jìn)行縮放導(dǎo)致的,。后來(lái)參考了系統(tǒng)camera中的代碼解決了這個(gè)問題,,特此記錄下來(lái)。每部手機(jī)中設(shè)置了好幾組預(yù)覽圖片的尺寸,、以及實(shí)際圖片的尺寸,。只有當(dāng)預(yù)覽圖的尺寸比例和實(shí)際圖片的比例相同的時(shí)候就不會(huì)出現(xiàn)變形的情況。下面一組數(shù)據(jù)是我測(cè)試中記錄的一組數(shù)據(jù):

02-09 19:00:40.831: D/tag(27507): pictureSizeValueString : 1280x960,1600x1200,2048x1536,2592x1456,2592x1936
02-09 19:00:40.831: D/tag(27507): previewSizeValueString : 176x144,320x240,352x288,640x480,720x480,720x576,848x480,1280x720

上面紅色部分標(biāo)記的都是具有相同比例的尺寸,,都為4:3,,而其他比例卻不盡相同。故我們只要在初始化camera的時(shí)候給其設(shè)置好相同比例的預(yù)覽圖尺寸和實(shí)際圖的尺寸,,并且設(shè)置你的SurfaceView或者VideoView的尺寸比例相同就行了,。但是要注意的是:魅族手機(jī)貌似取不到這些值,。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            //設(shè)置參數(shù)
            mCamera = Camera.open();
            //攝像頭畫面顯示在Surface上
            mCamera.setPreviewDisplay(surfaceHolder);
                       
            Parameters parameters = mCamera.getParameters();
        } catch (IOException e) {
            if (mCamera != null) mCamera.release();
            mCamera = null;
        }
                   
        if (mCamera == null) {
            finish();
            return ;
        }
        updateCameraParameters();
        mCamera.startPreview();  
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        try {
            //攝像頭畫面顯示在Surface上
            mCamera.setPreviewDisplay(surfaceHolder);
            updateCameraParameters();
            mCamera.startPreview();  
        } catch (IOException e) {
            if (mCamera != null) mCamera.release();
            mCamera = null;
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        //關(guān)閉預(yù)覽
        mCamera.release();
        //釋放資源
    }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
private void updateCameraParameters() {
        if (mCamera != null) {
            Camera.Parameters p = mCamera.getParameters();
                
            long time = new Date().getTime();
            p.setGpsTimestamp(time);
                
            Size pictureSize = findBestPictureSize(p);
            p.setPictureSize(pictureSize.width, pictureSize.height);
                
            // Set the preview frame aspect ratio according to the picture size.
            Size size = p.getPictureSize();
            PreviewFrameLayout frameLayout = (PreviewFrameLayout) findViewById(R.id.frame_layout);
            frameLayout.setAspectRatio((double) size.width / size.height);
                
            Size previewSize = findBestPreviewSize(p);
            p.setPreviewSize(previewSize.width,previewSize.height);
                
            mCamera.setParameters(p);
                
            int supportPreviewWidth = previewSize.width;
            int supportPreviewHeight = previewSize.height;
                
            int srcWidth = getScreenWH().widthPixels;
            int srcHeight = getScreenWH().heightPixels;
                
            int width = Math.min(srcWidth, srcHeight);
            int height = width * supportPreviewWidth / supportPreviewHeight ;
               
            mSur.setLayoutParams(new FrameLayout.LayoutParams(height, width));//
        }
    }
        
    private Size findBestPictureSize(Camera.Parameters parameters) {
        int  diff = Integer.MIN_VALUE;
        String pictureSizeValueString = parameters.get("picture-size-values");
             
         // saw this on Xperia
         if (pictureSizeValueString == null) {
             pictureSizeValueString = parameters.get("picture-size-value");
         }
             
         if(pictureSizeValueString == null) {
             return  mCamera.new Size(getScreenWH().widthPixels,getScreenWH().heightPixels);
         }
             
         Log.d("tag", "pictureSizeValueString : " + pictureSizeValueString);
         int bestX = 0;
         int bestY = 0;
            
            
         for(String pictureSizeString : COMMA_PATTERN.split(pictureSizeValueString))
         {
             pictureSizeString = pictureSizeString.trim();
                 
             int dimPosition = pictureSizeString.indexOf('x');
             if(dimPosition == -1){
                 Log.e(TAG, "Bad pictureSizeString:"+pictureSizeString);
                 continue;
             }
                 
             int newX = 0;
             int newY = 0;
                 
             try{
                 newX = Integer.parseInt(pictureSizeString.substring(0, dimPosition));
                 newY = Integer.parseInt(pictureSizeString.substring(dimPosition+1));
             }catch(NumberFormatException e){
                 Log.e(TAG, "Bad pictureSizeString:"+pictureSizeString);
                 continue;
             }
                
             Point screenResolution = new Point (getScreenWH().widthPixels,getScreenWH().heightPixels);
                 
             int newDiff = Math.abs(newX - screenResolution.x)+Math.abs(newY- screenResolution.y);
                 if(newDiff == diff)
                 {
                     bestX = newX;
                     bestY = newY;
                     break;
                 } else if(newDiff > diff){
                     if((3 * newX) == (4 * newY)) {
                         bestX = newX;
                         bestY = newY;
                         diff = newDiff;
                     }
                 }
             }
                 
         if (bestX > 0 && bestY > 0) {
            return mCamera.new Size(bestX, bestY);
         }
        return null;
    }
        
    private Size findBestPreviewSize(Camera.Parameters parameters) {
            
         String previewSizeValueString = null;
         int diff = Integer.MAX_VALUE;
         previewSizeValueString = parameters.get("preview-size-values");
             
         if (previewSizeValueString == null) {
             previewSizeValueString = parameters.get("preview-size-value");
         }
            
         if(previewSizeValueString == null) {  // 有些手機(jī)例如m9獲取不到支持的預(yù)覽大小   就直接返回屏幕大小
             return  mCamera.new Size(getScreenWH().widthPixels,getScreenWH().heightPixels);
         }
         Log.d("tag", "previewSizeValueString : " + previewSizeValueString);
         int bestX = 0;
         int bestY = 0;
            
         for(String prewsizeString : COMMA_PATTERN.split(previewSizeValueString))
         {
             prewsizeString = prewsizeString.trim();
                 
             int dimPosition = prewsizeString.indexOf('x');
             if(dimPosition == -1){
                 Log.e(TAG, "Bad prewsizeString:"+prewsizeString);
                 continue;
             }
                 
             int newX = 0;
             int newY = 0;
                 
             try{
                 newX = Integer.parseInt(prewsizeString.substring(0, dimPosition));
                 newY = Integer.parseInt(prewsizeString.substring(dimPosition+1));
             }catch(NumberFormatException e){
                 Log.e(TAG, "Bad prewsizeString:"+prewsizeString);
                 continue;
             }
                
             Point screenResolution = new Point (getScreenWH().widthPixels,getScreenWH().heightPixels);
                 
             int newDiff = Math.abs(newX - screenResolution.x)+Math.abs(newY- screenResolution.y);
                 
             if(newDiff == diff)
             {
                 bestX = newX;
                 bestY = newY;
                 break;
             } else if(newDiff < diff){
                 if((3 * newX) == (4 * newY)) {
                     bestX = newX;
                     bestY = newY;
                     diff = newDiff;
                 }
             }
         }
         if (bestX > 0 && bestY > 0) {
            return mCamera.new Size(bestX, bestY);
         }
        return null;
    }
        
     protected DisplayMetrics getScreenWH() {
        DisplayMetrics dMetrics = new DisplayMetrics();
        dMetrics = this.getResources().getDisplayMetrics();
        return dMetrics;
     }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式,、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙,。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào),。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多