2012-02-19 9 views
0

私のDroidXでは正常に動作するアンドロイドプロジェクトがありますが、Galaxyタブレットでは機能しません。サポートされていないカメラタイプ1200x728を示すログメッセージが表示されます。私はどこにカメラの解像度を設定していない、と私はそれがデバイスからのデフォルトの設定をつかむだけだと信じていました。 Galaxy Tabにロードしようとするとクラッシュするのはなぜですか?私はそのように私のカメラクラスをロードし、私の主な活動からアプリケーションがGalaxyタブレットでクラッシュする

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // requesting to turn the title OFF 
    //requestWindowFeature(Window.FEATURE_NO_TITLE); 

    // making it full screen 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 

    //Set Screen Orientation 
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    try{ 

     //Create Intance of Camera 
     camPreview = new CamLayer(this.getApplicationContext()); 

     //Create Instance of OpenGL 
     glView = new GLLayer(this); 

     //FrameLayOut for holding everything 
     FrameLayout frame = new FrameLayout(this); 
     // set as main view 
     setContentView(frame); 

     // add Camera to view 
     frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     frame.addView(glView); 


    } catch(Exception e){} 
} 

その後、私のカメラクラスは次のようになります。私はいくつかの問題があるべきだと思う

package com.eliddell.AR; 

    import android.content.Context; 
    import android.graphics.PixelFormat; 
    import android.hardware.Camera; 
    import android.hardware.Camera.Parameters; 
    import android.view.SurfaceHolder; 
    import android.view.SurfaceView; 

    public class CamLayer extends SurfaceView { 
      Camera camera; 
      SurfaceHolder previewHolder; 

    public CamLayer(Context context) 
    { 
     super(context); 
     previewHolder = this.getHolder(); 
     previewHolder.setType 
     (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     previewHolder.addCallback(surfaceHolderListener); 

    } 

    SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() { 

     public void surfaceCreated(SurfaceHolder holder) { 
      camera=Camera.open(); 
     try { 
      camera.setPreviewDisplay(previewHolder); 

     } 
      catch (Throwable e){ } 
     } 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
     { 
      Parameters params = camera.getParameters(); 
      params.setPreviewSize(width, height); 
      params.setPictureFormat(PixelFormat.JPEG); 
      camera.setParameters(params); 
      camera.startPreview(); 
     } 
     public void surfaceDestroyed(SurfaceHolder arg0) 
     { 
      camera.stopPreview(); 
      camera.release(); 
     } 

    }; 

    public void onResume() { 
     camera.startPreview(); 

    } 

    public void onPause() { 
     // TODO Auto-generated method stub 
     camera.stopPreview(); 
    } 

}

答えて

2

プレビューパラメータを設定します。以下を試してください。

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
if (isPreviewRunning) { 
camera.stopPreview(); 
} 
try{ 
Camera.Parameters p = camera.getParameters(); 
if(p!=null){ 
List<Size> sizes = p.getSupportedPreviewSizes(); 
Size optimalSize = getOptimalPreviewSize(sizes, w, h); 
p.setPreviewSize(optimalSize.width, optimalSize.height); 
camera.setParameters(p); 

camera.setPreviewDisplay(holder);; 

camera.startPreview(); 

} 
} catch (IOException e) { 
// TODO Auto-generated catch block 


e.printStackTrace(); 
} 

isPreviewRunning = true; 
} 

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { 
// TODO Auto-generated method stub 
final double ASPECT_TOLERANCE = 0.05; 
double targetRatio = (double) w/h; 
if (sizes == null) return null; 

Size optimalSize = null; 
double minDiff = Double.MAX_VALUE; 


int targetHeight = h; 

// Try to find an size match aspect ratio and size 
for (Size size : sizes) { 
double ratio = (double) size.width/size.height; 
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 
if (Math.abs(size.height - targetHeight) < minDiff) { 
optimalSize = size; 
minDiff = Math.abs(size.height - targetHeight); 
} 
} 


// Cannot find the one match the aspect ratio, ignore the requirement 

if (optimalSize == null) { 
minDiff = Double.MAX_VALUE; 

for (Size size : sizes) { 

if (Math.abs(size.height - targetHeight) < minDiff) { 
optimalSize = size; 
minDiff = Math.abs(size.height - targetHeight); 

} 
} 
} 

return optimalSize; 

} 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 



if (isPreviewRunning) { 
camera.stopPreview(); 
} 
try{ 
Camera.Parameters p = camera.getParameters(); 
if(p!=null){ 
List<Size> sizes = p.getSupportedPreviewSizes(); 
Size optimalSize = getOptimalPreviewSize(sizes, w, h); 
p.setPreviewSize(optimalSize.width, optimalSize.height); 
camera.setParameters(p); 

camera.setPreviewDisplay(holder);; 
camera.startPreview(); 

} 
} catch (IOException e) { 
// TODO Auto-generated catch block 


e.printStackTrace(); 
} 

isPreviewRunning = true; 
} 

private Size getOptimalPreviewSize(List sizes, int w, int h) { 
// TODO Auto-generated method stub 
final double ASPECT_TOLERANCE = 0.05; 
double targetRatio = (double) w/h; 
if (sizes == null) return null; 

Size optimalSize = null; 
double minDiff = Double.MAX_VALUE; 

int targetHeight = h; 

// Try to find an size match aspect ratio and size 
for (Size size : sizes) { 
double ratio = (double) size.width/size.height; 
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; 
if (Math.abs(size.height - targetHeight) < minDiff) { 
optimalSize = size; 
minDiff = Math.abs(size.height - targetHeight); 
} 
} 

// Cannot find the one match the aspect ratio, ignore the requirement 
if (optimalSize == null) { 
minDiff = Double.MAX_VALUE; 
for (Size size : sizes) { 
if (Math.abs(size.height - targetHeight) < minDiff) { 
optimalSize = size; 
minDiff = Math.abs(size.height - targetHeight); 
} 
} 
} 
return optimalSize; 

} 

あなたのsurfaceChanged()にこのコードを挿入します。 getOptimalPreviewSize()は、デバイスの解像度に応じてプレビューパラメータを設定するために使用されます。

+0

は、デバイスのネイティブカメラを取得するだけのコードのようですが、 – erik

+0

もしそうなら、androidmanifest.xmlにカメラを使用する許可を設定する必要があります – Dhruvisha

+0

私のマニフェストに私の許可が設定されています。私が正しいとしなかったらどんなデバイスでもクラッシュすると思う? – erik

関連する問題