2015-08-28 129 views
2

我在我的應用程序中使用目標sdk 22構建自定義相機。我已經通過我的應用程序在包括三星S5,三星Note 4和華爲登臺隊友7和圖像預覽很好。Android三星Galaxy S4自定義相機預覽失真

但是,當我通過我的應用程序在三星Galaxy S4(Android 5.0.1)打開相機,我得到相機預覽失真。以下是預覽的圖像:

enter image description here

我看到的計算器很多帖子,但他們對我的問題,拍攝圖像後,是拍攝照片前。

以下是我使用的預覽代碼:

  1. surfaceChanged方法

     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
         try { 
         holder.addCallback(this); 
          holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
          Camera.Parameters parameters = camera.getParameters(); 
          try { 
          List<Camera.Size> supportedSizes = null; 
          supportedSizes = Compatibility.getSupportedPreviewSizes(parameters); 
    
          //preview form factor 
          float ff = (float)w/h; 
          Log.d("", "Screen res: w:"+ w + " h:" + h + " aspect ratio:" + ff); 
    
          //holder for the best form factor and size 
          float bff = 0; 
          int bestw = 0; 
          int besth = 0; 
          Iterator<Camera.Size> itr = supportedSizes.iterator(); 
    
          while(itr.hasNext()) { 
           Camera.Size element = itr.next(); 
           //current form factor 
           float cff = (float)element.width/element.height; 
    
           Log.d("Camera", "Candidate camera element: w:"+ element.width + " h:" + element.height + " aspect ratio:" + cff); 
           int currentRes = element.width*element.height; 
           //ff = Asp ratio of current Screen 
           // cff = Asp ratio of current Size from Size list 
           // bff = Asp ratio of current best aspect ratio selected 
           if ((Math.abs(ff-cff) <= Math.abs(ff-bff))) { 
             bff=cff; 
             bestw = element.width; 
             besth = element.height; 
            } 
          } 
    
          Log.d("Camera", "Chosen camera element: w:"+ bestw + " h:" + besth + " aspect ratio:" + bff); 
          if ((bestw == 0) || (besth == 0)){ 
           Log.d("Camera", "Using default camera parameters!"); 
           bestw = 480; 
           besth = 320; 
           Log.d("Camera","Using default camera parameters! aspect ratio "+480/320); 
          } 
          parameters.setPreviewSize(bestw, besth); 
    
          //Handling camera angle 
          switch(this.getWindowManager().getDefaultDisplay().getRotation()){ 
          case 0: 
           camera.setDisplayOrientation(90); 
           break; 
          case 1: 
           camera.setDisplayOrientation(0); 
           break; 
          case 2: 
          case 3: 
           camera.setDisplayOrientation(180); 
           break; 
          } 
    
         } catch (Exception ex) { 
          parameters.setPreviewSize(480 , 320); 
         } 
    
         camera.setParameters(parameters); 
         camera.startPreview(); 
        } catch (Exception ex) { 
         ex.printStackTrace(); 
        } 
    } 
    
  2. Compatibility.getSupportedPreviewSizes(參數)方法

    @SuppressWarnings("unchecked") 
    public static List<Camera.Size> getSupportedPreviewSizes(Camera.Parameters params) { 
    List<Camera.Size> retList = null; 
    
    try { 
        Object retObj = mParameters_getSupportedPreviewSizes.invoke(params); 
        if (retObj != null) { 
         retList = (List<Camera.Size>)retObj; 
        } 
    } 
    catch (InvocationTargetException ite) { 
        /* unpack original exception when possible */ 
        Throwable cause = ite.getCause(); 
        if (cause instanceof RuntimeException) { 
         throw (RuntimeException) cause; 
        } else if (cause instanceof Error) { 
         throw (Error) cause; 
        } else { 
         /* unexpected checked exception; wrap and re-throw */ 
         throw new RuntimeException(ite); 
        } 
    } catch (IllegalAccessException ie) { 
        //System.err.println("unexpected " + ie); 
    } 
    return retList; 
    } 
    

我將不勝感激任何形式的幫助。

在此先感謝。

+0

你會選擇哪一個預覽大小? –

+0

預覽尺寸爲480×320 – Sateesh

+0

您的意思是,它回落到480x320,因爲沒有支持的尺寸不夠好?據我所知,問題在於S4不支持該決議。使用不支持的分辨率的結果是不可預知的。 –

回答

0

在Samsung Note IV上,我意識到您不應該使用setPreviewSize(),即使您從getSupportedPreviewSizes()獲得的尺寸應該合法且可用。

這是三星的低級相機軟件中的一個尷尬的錯誤。 Uncool,三星!它僅通過一些設備進行再現。

然而,仍然拍攝是無可挑剔的。 所以,這裏是我的代碼:

Camera.Parameters parameters = camera.getParameters(); 

    boolean currentPreviewSettingsAreValid = parameters != null; 
    currentPreviewSettingsAreValid = currentPreviewSettingsAreValid && parameters.getPreviewSize() != null; 
    currentPreviewSettingsAreValid = currentPreviewSettingsAreValid && parameters.getPreviewSize().width > 0 && parameters.getPreviewSize().height > 0; 

    if (parameters != null && !currentPreviewSettingsAreValid) { 
     Camera.Size bestSize = getBestSizeOfSupported(parameters.getSupportedPreviewSizes(), width, height); 

     if (bestSize != null) { 
      parameters.setPreviewSize(bestSize.width, bestSize.height); 
     } 
    } 

希望能有助於任何人

+0

你可以開放源碼這個相機代碼多一點? – rupinderjeet