Android前置闪光灯使用屏幕美白

时间:2017-02-08 01:27:19

标签: android camera

我想使用类似于Fornt Flash的前置摄像头“闪光”技巧来捕捉图像 我知道它可能涉及到相机预览之上的另一层 建议herehere 任何人都可以发布包含相机的工作解决方案吗?

感谢。

1 个答案:

答案 0 :(得分:0)

由我自己找到(以及朋友的一点帮助) 基本上你需要创建一个闪存模拟器视图,其背景为alpha为0.7,可见性消失 点击时你需要:

显示视图 - View.VISIBLE

flashEmulator.setVisibility(View.VISIBLE);

将screenBrightness调整为最大值

Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();

lastScreenBrightness = params.screenBrightness;
params.screenBrightness = 1F;
window.setAttributes(params);

将flash模拟器的背景转换为HSV

int color = Color.WHITE;
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 1f;
color = Color.HSVToColor(hsv);
flashEmulator.setBackgroundColor(color);

完整的Android前端Flash应用程序代码

清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.frontflash.android">
<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.front" android:required="false" />
<uses-feature android:name="android.hardware.camera.flash" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/black"
   tools:context=".MainActivity">

<com.frontflash.android.CameraPreview
    android:id="@+id/cameraPreview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<FrameLayout
    android:visibility="gone"
    android:alpha="0.7"
    android:id="@+id/flashEmulator"
    android:background="@android:color/white"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="On"
    android:id="@+id/button"
    android:layout_centerVertical="false"
    android:layout_centerHorizontal="true" />

MainActivity.java

package com.frontflash.android;

import android.app.Activity;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;

public class MainActivity extends Activity {
    private Button button;
    private Camera camera;
    private boolean isFlashOn;
    private FrameLayout flashEmulator;
    private float lastScreenBrightness;
    private CameraPreview cameraPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        int fullScreen = WindowManager.LayoutParams.FLAG_FULLSCREEN;

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(fullScreen, fullScreen);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        flashEmulator = (FrameLayout) findViewById(R.id.flashEmulator);
        cameraPreview = (CameraPreview)findViewById(R.id.cameraPreview);
        startCamera();

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isFlashOn) {
                    turnOffFlash();
                    button.setText("On");
                } else {
                    turnOnFlash();
                    button.setText("Off");
                }
            }
        });
    }

    private void startCamera() {
        if (camera == null) {
            try {
                camera = Camera.open(1);
                Camera.Parameters params =  camera.getParameters();

                params.setPreviewSize(1280, 720);
                camera.setDisplayOrientation(90);
                camera.setParameters(params);
                cameraPreview.connectCamera(camera);
                cameraPreview.startPreview();
            } catch (Exception e) {
            }
        }
    }

    private void turnOnFlash() {
        if (!isFlashOn) {
            int color = Color.WHITE;
            float[] hsv = new float[3];
            final Window window = getWindow();
            final WindowManager.LayoutParams params = window.getAttributes();

            lastScreenBrightness = params.screenBrightness;
            params.screenBrightness = 1F;
            window.setAttributes(params);
            flashEmulator.setVisibility(View.VISIBLE);
            Color.colorToHSV(color, hsv);
            hsv[2] *= 1f;
            color = Color.HSVToColor(hsv);
            flashEmulator.setBackgroundColor(color);
            isFlashOn = true;
        }
    }

    private void turnOffFlash() {
        if (isFlashOn) {
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();

            params.screenBrightness = lastScreenBrightness;
            window.setAttributes(params);
            flashEmulator.setVisibility(View.GONE);
            isFlashOn = false;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onPause() {
        super.onPause();
        turnOffFlash();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        startCamera();
    }

    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
            camera = null;
        }
    }
}

CameraPreview.java

package com.frontflash.android;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by sagi on 2/8/2017.
 */

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private Camera camera;
    private SurfaceHolder surfaceHolder;
    private final String LOG_TAG = "CameraPreview";

    public CameraPreview(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public CameraPreview(Context context) {
        super(context);
    }

    public void connectCamera(Camera camera) {
        this.camera = camera;
        surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);
        startPreview();
    }

    public void releaseCamera() {
        if (camera != null) {
            stopPreview();
            camera = null;
        }
    }

    void startPreview() {
        if (camera != null && surfaceHolder.getSurface() != null) {
            try {
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
            } catch (Exception e) {
                Log.e(LOG_TAG, String.format("Error setting preview display: %s", e.getMessage()));
            }
        }
    }

    void stopPreview() {
        if (camera != null) {
            try {
                camera.stopPreview();
            } catch (Exception e) {
                Log.e(LOG_TAG, String.format("Error stopping preview: %s", e.getMessage()));
            }
        }
    }

    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        startPreview();
    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        stopPreview();
        startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        stopPreview();
    }
}