Android:如何获取内置相机应用程序的默认相机设置

发布于 2025-01-06 15:17:35 字数 2974 浏览 1 评论 0原文

我看过很多教程和信息,但我找不到任何一个地方如何将现有相机应用程序的默认设置用于任何其他自定义相机应用程序。我在内置相机应用程序中看到了图像的清晰度和焦点非常精细。现在我正在使用自定义功能创建自己的应用程序,但我仍然无法使其清晰且不模糊...我不想使用相机的 Intent 技术,因为之后我必须进行一些图像处理。

我使用过缩放,但奇怪的是缩放无法正常工作...就像它在内置相机应用程序中工作一样

,这是我的表面更改代码

   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;
}  

请让我知道如何使相机预览与内置应用程序完全相同。

I have seen lots of tutorial and information but i could not find any single place how to use the default settings of the existing camera application into any other customized camera application. I have seen the sharpness of the image and its focus is very fine in the built-in camera application. Now i am creating my own application with my customized features but i am still unable to make it sharp and non-blurry... I dont want to use Intent technique of the camera because i have to do some image processing afterward.

I have used zooming but strangely zoom is not properly working ...like it works in built-in camera application

here is my surface change code

   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;
}  

Kindly let me know how to make the camera preview exactly same as the built in application one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百思不得你姐 2025-01-13 15:17:36

您是说全屏相机预览吗?

我使用此代码:

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);

将应用程序设置为全屏并隐藏标题和状态栏。
第二个片段将我的覆盖层(扩展视图)添加到主布局中。

这是我的 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);
}

希望有帮助

You mean a fullscreen camera preview?

I use this code:

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

and this:

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

the first snippet sets the app to fullscreen and hide title and status bar.
the second snipppet adds my overlay (extended View) to the main layout.

Here my xml and java code:
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);
    }
}

And my activity:

@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);
}

Hope it helps

岁吢 2025-01-13 15:17:36

我和你遇到了同样的问题。在阅读了内置相机应用程序的源代码,并比较了内置相机和我自己的相机的对焦处理后,我意识到问题出在自动对焦上。

所以试试这个:

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

这使得结果图像像内置相机一样清晰。
该文档位于此处

I encountered the same problem with you. After reading the source code of the builtin camera app, and comparing the focus processing of builtin camera and my own camera, I realized the problem is on autofocus.

So try this:

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

which makes the result image as sharp as builtin camera.
The documents is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文