2012-02-19 176 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