2

我制作了一个手电筒应用程序,可以通过按钮打开相机的 LED 灯。如果可能的话,我希望能够用搜索栏改变 LED 灯的亮度。

如果这有帮助,这是我的按钮中的代码:

public class FlashLightActivity extends Activity {


private boolean isLighOn = false;
private Camera camera;
private Button button;

@Override
protected void onStop() {
    super.onStop();
    if (camera != null) {
        camera.release();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.buttonFlashlight);
    Context context = this;
    PackageManager pm = context.getPackageManager();

    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Log.e("err", "Device has no camera!");
        return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            if (isLighOn) {
                Log.i("info", "torch is turn off!");
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                isLighOn = false;
            } else {
                Log.i("info", "torch is turn on!");
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                isLighOn = true;
            }
        }
    });
    }
 }
4

4 回答 4

1

检查在增加/减少图像亮度中给出的示例

并在搜索栏进度更改上执行如下所示

 int brightness;
 SeekBar seekbarbrightness=(SeekBar)findViewById(R.id.seekBar1);
 seekbarbrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

         @Override
         public void onStopTrackingTouch(SeekBar arg0) {
          // TODO Auto-generated method stub

         }

         @Override
         public void onStartTrackingTouch(SeekBar arg0) {
          // TODO Auto-generated method stub

         }

         @Override
         public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

             // TODO Auto-generated method stub

             brightness=progress;
             Bitmap bitmap=doBrightness(originalbitmap,brightness);
             imageView.setImageBitmap(bitmap);
         }
        });
于 2012-11-03T05:56:21.187 回答
1

据我自己的搜索所见(我还想为不同的应用程序控制相机 LED 的亮度),只能使用相机界面使用手电筒模式关闭或打开 LED。

现在,还可以在 Java 级别编写模拟 PWM(脉冲宽度调制)以在 TORCH_MODE 和 OFF 之间切换。但是我这样做的测试产生了太多的闪烁。

然后,我还尝试使用 NDK 在 Native 级别查看类似的方法,但似乎与相机(和 LED)的唯一接口是通过 Java 层。

2013-10-05 更新

下面是用于相机 LED 的 PWM 控制的 Java 代码。它可以工作,但闪烁使其通常无法使用。可能还有更多可以添加的优化,但不确定它们是否会对闪烁产生重大影响。

2013-12-15 更新

根据 Class Stacker 的反馈,我已经删除了无法实现的线程优先级,并且还改进了 PWM 延迟以单独使用 Thread.sleep 函数。测试显示略有改善,但闪烁仍然存在。请注意,可以减少睡眠值,但相机 API 调用需要相当长的时间才能返回,因此减少 PWM 睡眠不会对闪烁产生任何影响。

package com.whimsicalotter.ledpwmexample;

import java.util.List;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.util.Log;

public class LedPwmController {

private static String TAG = "LedPwmController";

private Camera m_camera = null;
private boolean m_torchSupported = false;

private LedPwmThread m_ledPwmThread = null;

private final int m_pwmMax = 10;
private int m_pwmValue = m_pwmMax;


public void init() {
    initCamera();

    // Start the thread
    m_ledPwmThread = new LedPwmThread();
    m_ledPwmThread.setRunning(true);
    m_ledPwmThread.start();
}

public void shutdown() {
    // Join the thread
    boolean retry = true;

    if (m_ledPwmThread != null) {
        m_ledPwmThread.setRunning(false);

        while (retry) {
            try {
                m_ledPwmThread.join();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

    destroyCamera();
}

/**
 * Set the LED brightness 0.0f to 1.0f.
 * @param brightness
 */
public void setLedBrightness(float brightness) {
    if( brightness < 0.0f ) brightness = 0.0f;
    if( brightness > 1.0f ) brightness = 1.0f;

    // Now convert to integer
    m_pwmValue = (int) Math.floor((brightness * (float)m_pwmMax));

    Log.i(TAG, "LED Brightness set:" + brightness + " == " + m_pwmValue);
}



private void initCamera() {
    if( m_camera == null ) {
        Log.i(TAG, "Opening Camera...");

        m_camera = Camera.open();

        m_torchSupported = isTorchSupported();

        Log.i(TAG, "Camera is open. Torch Supported = " + m_torchSupported);
    }
}

private void destroyCamera() {
    // switch off led
    ledOff();

    if( m_camera != null ) {
        Log.i(TAG, "Releasing Camera...");
        m_camera.release();
        m_camera = null;
    }
}

private boolean isTorchSupported() {
    if( m_camera != null ) {
        Parameters params = m_camera.getParameters();
        List<String> flashmodes = params.getSupportedFlashModes();
        if( flashmodes == null ) {
            return false;
        }

        return flashmodes.contains( Parameters.FLASH_MODE_TORCH );
    }

    return false;
}

private void ledOn() {
    if( !m_torchSupported ) {
        return;
    }

    if( m_camera != null ) {
        Parameters params = m_camera.getParameters();
        params.setFlashMode( Parameters.FLASH_MODE_TORCH );
        m_camera.setParameters(params);
    }
}

private void ledOff() {
    if( !m_torchSupported ) {
        return;
    }

    if( m_camera != null ) {
        Parameters params = m_camera.getParameters();
        params.setFlashMode( Parameters.FLASH_MODE_OFF );
        m_camera.setParameters(params);
    }
}


private class LedPwmThread extends Thread {

    private boolean m_running = false;

    public void setRunning(boolean running) {
        m_running = running;
    }

    @Override
    public void run() {
        if( m_camera != null && m_torchSupported ) {
            Parameters params = m_camera.getParameters();

            while( m_running ) {

                //ledOn();
                if( m_pwmValue > 0 ) {
                    params.setFlashMode( Parameters.FLASH_MODE_TORCH);
                    m_camera.setParameters(params);


                    try {
                        Thread.sleep(10*m_pwmValue,0);
                    } catch (InterruptedException e) {
                    }
                }

                //ledOff();
                if( m_pwmValue < m_pwmMax ) {
                    params.setFlashMode( Parameters.FLASH_MODE_OFF );
                    m_camera.setParameters(params);


                    try {
                        Thread.sleep(10*(m_pwmMax-m_pwmValue),0);
                    } catch (InterruptedException e) {
                    }   
                }

            }
        }
    }
}
}
于 2013-10-04T16:43:28.117 回答
0

onCreate 方法中的简单代码如下:

SeekBar brightnesSseeker;   
brightnesSseeker = (SeekBar) findViewById(R.id.brightnesSseeker);

    brightnesSseeker
            .setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
                public void onStopTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onStartTrackingTouch(SeekBar seekBar) {
                    // Nothing handled here
                }

                public void onProgressChanged(SeekBar seekBar,
                        int progress, boolean fromUser) {
                    // Set the minimal brightness level
                    // if seek bar is 20 or any value below
                    if (progress <= 20) {
                        // Set the brightness to 20
                        screenBrightness(20);
                    } else {
                        // Set brightness variable based on the progress bar
                        screenBrightness(progress);
                    }
                    stobeVariable=progress*10;
                }
            });

在课堂上写下这个函数:

private void screenBrightness(double newBrightnessValue) {
    /*
     * WindowManager.LayoutParams settings = getWindow().getAttributes();
     * settings.screenBrightness = newBrightnessValue;
     * getWindow().setAttributes(settings);
     */
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    float newBrightness = (float) newBrightnessValue;
    lp.screenBrightness = newBrightness / (float) 255;
    getWindow().setAttributes(lp);
}

你也可以看到这个链接:这是一个链接

于 2014-02-17T12:51:08.903 回答
0

我为你的问题举了一个完整的例子:

主要活动 :-

public class AndroidScreenBrightness extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);

     SeekBar BackLightControl = (SeekBar)findViewById(R.id.backlightcontrol);
     final TextView BackLightSetting = (TextView)findViewById(R.id.backlightsetting);

     BackLightControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){

 @Override
 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
  // TODO Auto-generated method stub
  float BackLightValue = (float)arg1/100;
  BackLightSetting.setText(String.valueOf(BackLightValue));

  WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
  layoutParams.screenBrightness = BackLightValue;
  getWindow().setAttributes(layoutParams);


 }

 @Override
 public void onStartTrackingTouch(SeekBar arg0) {
  // TODO Auto-generated method stub

 }

 @Override
 public void onStopTrackingTouch(SeekBar arg0) {
  // TODO Auto-generated method stub

 }});
 }
}

主要的.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="@string/hello"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Set BackLight of the App"
   />
<SeekBar
   android:id="@+id/backlightcontrol"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_margin="10px"
   android:max="100"
   android:progress="50"
   />
<TextView 
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/backlightsetting"
   android:text="0.50"
   />
</LinearLayout>
于 2012-11-03T05:59:04.197 回答