2012-02-19 181 views
4

我已經看過很多教程和信息,但是我找不到任何一個地方如何將現有攝像頭應用程序的默認設置應用到任何其他自定義攝像頭應用程序中。我已經看到了圖像的清晰度,並且在內置相機應用程序中它的焦點非常好。現在我正在用我的自定義功能創建自己的應用程序,但我仍然無法使其變得清晰和不模糊......我不想使用相機的Intent技術,因爲之後我必須進行一些圖像處理。Android:如何獲取內置攝像頭應用程序的默認攝像頭設置

我已經使用縮放但奇怪的是變焦無法正常工作......就像它工作在內置攝像頭的應用

這裏是我的表面變化的代碼

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
{ 
    Log.e(TAG, "surfaceChanged"); 

    // XXX stopPreview() will crash if preview is not running 
    if (mPreviewRunning) { 
     mCamera.stopPreview(); 
    } 


    Camera.Parameters params = mCamera.getParameters(); 
    List<Camera.Size> sizes = params.getSupportedPreviewSizes(); 
    mFrameWidth = w; 
    mFrameHeight = h; 

    // selecting optimal camera preview size 
    { 
     double minDiff = Double.MAX_VALUE; 
     for (Camera.Size size : sizes) 
     { 
      if (Math.abs(size.height - h) < minDiff) 
      { 
       mFrameWidth = size.width; 
       mFrameHeight = size.height; 
       minDiff = Math.abs(size.height - h); 
      } 
     } 
    } 

    try 
    { 

     //params.set("rotation", 180); 
     //params.set("orientation", "landscape"); 
     //params.set("auto", "WHITE_BALANCE_AUTO");//WHITE_BALANCE_AUTO 




     Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 

     if(display.getRotation() == Surface.ROTATION_0) 
     { 
      params.setPreviewSize(mFrameHeight, mFrameWidth);       
      mCamera.setDisplayOrientation(90); 
     } 

     if(display.getRotation() == Surface.ROTATION_90) 
     { 
      params.setPreviewSize(mFrameWidth, mFrameHeight);       
     } 

     if(display.getRotation() == Surface.ROTATION_180) 
     { 
      params.setPreviewSize(mFrameHeight, mFrameWidth);    
     } 

     if(display.getRotation() == Surface.ROTATION_270) 
     { 
      params.setPreviewSize(mFrameWidth, mFrameHeight); 
      mCamera.setDisplayOrientation(180); 
     } 

     if(params.isZoomSupported()) 
     { 

      Log.e(TAG, params.getZoom()+"surfaceChanged camer zoom"+params.getMinExposureCompensation()); 
      params.setZoom(params.getMaxZoom()); 
      params.setExposureCompensation(1); 
        // params.setColorEffect("none"); 
      params.setWhiteBalance(params.WHITE_BALANCE_AUTO); 
      params.setFocusMode(params.FOCUS_MODE_AUTO); 
      params.setSceneMode(params.SCENE_MODE_ACTION); 

     } 

     params.set("auto", "FOCUS_MODE_AUTO"); 

     params.setPreviewSize(mFrameWidth,mFrameHeight); 
     mCamera.setParameters(params); 

     mCamera.setPreviewDisplay(holder); 
    } 
    catch (IOException e) 
    { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    mCamera.startPreview(); 
    mPreviewRunning = true; 
} 

請讓我知道如何使相機預覽與內置的應用程序完全相同。

+0

你看過Android Camera Application代碼嗎?您可以將其用於您將支持的最低版本的操作系統,並將其用作基礎。請記住,每個操作系統升級添加的功能,並且您的設備的製造商可能支持其他設備上可能存在或不存在的功能。 – Idistic 2012-02-24 04:17:17

回答

2

你的意思是全屏相機預覽?

我用這個代碼:

this.requestWindowFeature(Window.FEATURE_NO_TITLE); //no title 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //no status bar, etc 

這:

setContentView(R.layout.main); 
    addContentView(overlay, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
((FrameLayout) findViewById(R.id.preview)).addView(preview); 

第一片段設置的應用程序切換到全屏模式,隱藏標題和狀態欄。 第二個snipppet將我的覆蓋圖(擴展視圖)添加到主佈局。

這裏我的XML和Java代碼: 的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <FrameLayout 
     android:id="@+id/preview" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

Overlay.java:

class Overlay extends View { 
    String text = ""; 
    String textBearing = "Bearing: "; 

    public Overlay(Context context) { 
      super(context); 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(Color.WHITE); 
     paint.setTextSize(16); 
     canvas.drawText(text, 20, 20, paint); 
     canvas.drawText(textBearing, 20, 50, paint); 
     super.onDraw(canvas); 
    } 
} 

我的活動:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); //no title 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); //fullscreen 
    overlay = new Overlay(this); 
    setContentView(R.layout.main); 
    addContentView(overlay, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    camera = getCameraInstance(); //camera.open(); 
    preview = new Preview(this, camera); 
    ((FrameLayout) findViewById(R.id.preview)).addView(preview); 
} 

希望它可以幫助

+0

您的代碼是否佔用了酒吧所佔用的空間?我有一個類似的解決方案,但我必須得到酒吧的高度,並添加它作爲填充。 – Johnny 2014-12-31 03:20:38

0

I en反駁了同樣的問題。在閱讀內置相機應用程序的源代碼,並比較內置相機和我自己的相機的焦點處理之後,我意識到問題出在自動對焦上。

那麼試試這個:

mCamera.autoFocus(new Camera.AutoFocusCallback() { 
     @Override 
     public void onAutoFocus(boolean success, Camera camera) { 
      mCamera.takePicture(null, null, mPicture); 
     } 
    }); 

這使得結果圖像那樣尖銳內置攝像頭。 這些文件是here