Setting the background of an xml layout to camera in android

半世苍凉 提交于 2021-02-08 06:56:21

问题


I have a project, where i have a xml layout, with buttons and all of that in it, and i need the background to be the camera, so the preview is behind the buttons, how can i do that?


回答1:


Here is xml from my project:

<?xml version="1.0" encoding="UTF-8"?>
<!--
     Copyright (C) 2008 ZXing authors Licensed under the Apache License, 
    Version 2.0 (the "License"); you may not use this file except in compliance 
    with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
    Unless required by applicable law or agreed to in writing, software distributed 
    under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 
    OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
    the specific language governing permissions and limitations under the License.


-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF8090A0" >

    <SurfaceView
        android:id="@+id/preview_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <com.x09soft.scanner.zxing.ViewfinderView
        android:id="@+id/viewfinder_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/transparent" />

    <ImageButton
        android:id="@+id/btn_flash"
        android:background="@drawable/flash_off"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_gravity="right|center_vertical"/>

</FrameLayout>

CaptureActivity and ViewfinderView your may see (as said above) here

As said in documentation to CaptureActivity:

This activity opens the camera and does the actual scanning on a background thread. It draws a viewfinder to help the user place the barcode correctly, shows feedback as the image processing is happening, and then overlays the results when a scan is successful.

ViewFinderView:

This view is overlaid on top of the camera preview. It adds the viewfinder rectangle and partial transparency outside it, as well as the laser scanner animation and result points.

If you don't want to draw any sharpes just not use ViewfinderView.

Take a look to CaptureActivity init camera method, maybe it'll be helpful to you.

private void initCamera(SurfaceHolder surfaceHolder) {
        try {
            cameraManager.openDriver(surfaceHolder);
            // Creating the handler starts the preview, which can also throw a
            // RuntimeException.
            if (handler == null) {
                handler = new CaptureActivityHandler(this, decodeFormats,
                        characterSet, cameraManager);
            }
        } catch (IOException ioe) {
            Log.w(TAG, ioe);
            displayFrameworkBugMessageAndExit();
        } catch (RuntimeException e) {
            // Barcode Scanner has seen crashes in the wild of this variety:
            // java.?lang.?RuntimeException: Fail to connect to camera service
            Log.w(TAG, "Unexpected error initializing camera", e);
            displayFrameworkBugMessageAndExit();
        }
    }

SurfaceHadler is created in resume() method:

 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();

In addition check this link



来源:https://stackoverflow.com/questions/12733079/setting-the-background-of-an-xml-layout-to-camera-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!