How to superimpose power information and device signal status on the Android platform to the GB28181 access terminal in real time

technical background

We implemented GB28181 device access on the Android platform, collected camera and microphone data, and used them in mobile soldiers, smart vehicles, smart security, smart home, industrial simulation and other industries, and found that most scenarios have more and more requirements for video watermarking. The higher it is, the transition from the previous fixed-position static text watermarks, png watermarks, etc. to dynamic watermark requirements.

In this article, what we want to discuss is how to superimpose the power and device signal status on the video view, in addition to the regular time, latitude and longitude information to obtain png watermarks.

How to Obtain Battery Information

The following three methods can be used to obtain power information on the Android platform:

  1. Obtain power information through the BatteryManager class:

You can use the Context.getSystemService() method to get the BatteryManager instance, and use the instance's getIntExtra() method to get the battery information. The specific code is as follows:

 BatteryManager batteryManager = (BatteryManager) getSystemService(Context.BATTERY_SERVICE);  
 int batteryLevel = batteryManager.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);  
 int batteryScale = batteryManager.getIntExtra(BatteryManager.EXTRA_SCALE, -1);  
 int batteryPlugType = batteryManager.getIntExtra(BatteryManager.EXTRA_PLUGED, -1);  
 boolean isCharging = batteryManager.getIntExtra(BatteryManager.EXTRA_CHARGING, 0) == 1;
  1. Obtain power information through the PowerManager class:

You can use the Context.getSystemService() method to get the PowerManager instance, and use the instance's isDeviceIdle() and isPowerSaveMode() methods to get the power information. The specific code is as follows:

 PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);  
 boolean isDeviceIdle = powerManager.isDeviceIdle();  
 boolean isPowerSaveMode = powerManager.isPowerSaveMode();
  1. Obtain power information through the UsageStatsManager class:

You can use the Context.getSystemService() method to get the UsageStatsManager instance, and use the queryStats() method of the instance to get the power information. The specific code is as follows:

 UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);  
 long time = System.currentTimeMillis();  
 UsageStats stats = usageStatsManager.queryStats(UsageStatsManager.INTERVAL_DAILY, time);  
 long totalTime = stats.getTotalTime();  
 long screenTime = stats.getScreenTime();  
 float batteryLevel = (totalTime * 100) / screenTime;

How to get device signal

To get the signal strength of an Android device, you can use the getSignalStrength() method in the TelephonyManager class. Here is a sample code snippet that demonstrates how to get the signal strength of a device:

 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
 int signalStrength = telephonyManager.getSignalStrength();

This code will return the signal strength of the device in dBm (decibel milliwatts). If the device does not have a calling card, the return value is int min(0).

Note that to use the TelephonyManager class, you need to add the following permissions in the AndroidManifest.xml file:

 <uses-permission android:name="android.permission.READ_PHONE_STATE" />

This permission allows your application to access the device's phone state and information.

How to superimpose the device power information and device signal status on the view

Superimpose the power of the device and the real-time signal status of the device. In fact, what is called is our dynamic text watermark. By generating a TextBitmap, and then copying the text_timestamp_buffer_ from the bitmap, it is delivered to the jni layer through our designed PostLayerImageRGBA8888ByteBuffer().

private int postTimestampLayer(int index, int left, int top) {

    Bitmap text_bitmap = makeTextBitmap(makeTimestampString(), getFontSize(),
            Color.argb(255, 0, 0, 0), true, Color.argb(255, 255, 255, 255),true);

    if (null == text_bitmap)
        return 0;

    if ( text_timestamp_buffer_ != null) {
        text_timestamp_buffer_.rewind();

        if ( text_timestamp_buffer_.remaining() < text_bitmap.getByteCount())
            text_timestamp_buffer_ = null;
    }

    if (null == text_timestamp_buffer_ )
        text_timestamp_buffer_ = ByteBuffer.allocateDirect(text_bitmap.getByteCount());

    text_bitmap.copyPixelsToBuffer(text_timestamp_buffer_);

    int scale_w = 0, scale_h = 0, scale_filter_mode = 0;

    libPublisher.PostLayerImageRGBA8888ByteBuffer(handle_, index, left, top, text_timestamp_buffer_, 0,
            text_bitmap.getRowBytes(), text_bitmap.getWidth(), text_bitmap.getHeight(),
            0, 0, scale_w, scale_h, scale_filter_mode,0);

    int ret = scale_h > 0? scale_h : text_bitmap.getHeight();

    text_bitmap.recycle();

    return ret;
}

text watermark

The text watermark will not go into details, the main attention is the size, color and position of the text.

private int postText1Layer(int index, int left, int top) {
    Bitmap text_bitmap = makeTextBitmap("文本水印一", getFontSize()+8,
            Color.argb(255, 200, 250, 0),
            false, 0,false);

    if (null == text_bitmap)
        return 0;

    ByteBuffer buffer = ByteBuffer.allocateDirect(text_bitmap.getByteCount());
    text_bitmap.copyPixelsToBuffer(buffer);

    libPublisher.PostLayerImageRGBA8888ByteBuffer(handle_, index, left, top, buffer, 0,
            text_bitmap.getRowBytes(), text_bitmap.getWidth(), text_bitmap.getHeight(),
            0, 0, 0, 0, 0,0);

    int ret = text_bitmap.getHeight();

    text_bitmap.recycle();

    return ret;
}

The final delivery interface is designed as follows, and the interface will not be described in detail. Almost all the processing you expect for images has been covered:

/**
   * 投递层RGBA8888图像,如果不需要Aplpha通道的话, 请使用RGBX8888接口, 效率高
   *
   * @param index: 层索引, 必须大于等于0, 注意:如果index是0的话,将忽略Alpha通道
   *
   * @param left: 层叠加的左上角坐标, 对于第0层的话传0
   *
   * @param top: 层叠加的左上角坐标, 对于第0层的话传0
   *
   * @param rgba_plane: rgba 图像数据
   *
   * @param offset: 图像偏移, 这个主要目的是用来做clip的, 一般传0
   *
   * @param row_stride: stride information
   *
   * @param width: width, 必须大于1, 如果是奇数, 将减1
   *
   * @param height: height, 必须大于1, 如果是奇数, 将减1
   *
   * @param  is_vertical_flip: 是否垂直翻转, 0不翻转, 1翻转
   *
   * @param  is_horizontal_flip:是否水平翻转, 0不翻转, 1翻转
   *
   * @param  scale_width: 缩放宽,必须是偶数, 0或负数不缩放
   *
   * @param  scale_height: 缩放高, 必须是偶数, 0或负数不缩放
   *
   * @param  scale_filter_mode: 缩放质量, 传0使用默认速度,可选等级范围是:[1,3],值越大缩放质量越好, 但速度越慢
   *
   * @param  rotation_degree: 顺时针旋转, 必须是0, 90, 180, 270, 注意:旋转是在缩放, 垂直/水品反转之后再做, 请留意顺序
   *
   * @return {0} if successful
   */
  public native int PostLayerImageRGBA8888ByteBuffer(long handle, int index, int left, int top,
                       ByteBuffer rgba_plane, int offset, int row_stride, int width, int height,
                       int is_vertical_flip,  int is_horizontal_flip,
                       int scale_width,  int scale_height, int scale_filter_mode,
                       int rotation_degree);

For the display control of the above watermark, we package and process it through LayerPostThread:

/*
 * LayerPostThread实现动态水印封装
 * Author: https://daniusdk.com
 */
class LayerPostThread extends Thread
{
  private final int update_interval = 400; // 400 毫秒
  private volatile boolean is_exit_ = false;
  private long handle_ = 0;
  private int width_  = 0;
  private int height_ = 0;
  private volatile boolean is_text_ = false;
  private volatile boolean is_picture_ = false;
  private volatile boolean clear_flag_ = false;

  private final int timestamp_index_ = 1;
  private final int text1_index_ = 2;
  private final int text2_index_ = 3;
  private final int picture_index_ = 4;
  private final int rectangle_index_ = 5;

  ByteBuffer text_timestamp_buffer_ = null;
  ByteBuffer rectangle_buffer_ = null;

  @Override
  public void run() {
      text_timestamp_buffer_ = null;
      rectangle_buffer_ = null;

      if (0 == handle_)
          return;

      boolean is_posted_pitcure = false;
      boolean is_posted_text1 = false;
      boolean is_posted_text2 = false;

      int rectangle_aplha = 0;

      while(!is_exit_) {
          long t = SystemClock.elapsedRealtime();

          if (clear_flag_) {
              clear_flag_ = false;
              is_posted_pitcure = false;
              is_posted_text1 = false;
              is_posted_text2 = false;

              if (!is_text_ || !is_picture_) {
                  rectangle_aplha = 0;
                  libPublisher.RemoveLayer(handle_, rectangle_index_);
              }
          }

          int cur_h = 8;
          int ret = 0;

          if (!is_exit_ && is_text_) {
              ret = postTimestampLayer(timestamp_index_, 0, cur_h);
              if ( ret > 0 )
                  cur_h = align(cur_h + ret + 2, 2);
          }

          if(!is_exit_&& is_text_&&!is_posted_text1) {
              cur_h += 6;
              ret = postText1Layer(text1_index_, 0, cur_h);
              if ( ret > 0 ) {
                  is_posted_text1 = true;
                  cur_h = align(cur_h + ret + 2, 2);
              }
          }

          if (!is_exit_ && is_picture_ && !is_posted_pitcure) {
              ret = postPictureLayer(picture_index_, 0, cur_h);
              if ( ret > 0 ) {
                  is_posted_pitcure = true;
                  cur_h = align(cur_h + ret + 2, 2);
              }
          }

          if(!is_exit_&& is_text_&&!is_posted_text2) {
              postText2Layer(text2_index_);
              is_posted_text2 = true;
          }

          // 这个是演示一个矩形, 不需要可以屏蔽掉
          if (!is_exit_ && is_text_ && is_picture_) {
                  postRGBRectangle(rectangle_index_, rectangle_aplha);
                  rectangle_aplha += 8;
                  if (rectangle_aplha > 255)
                      rectangle_aplha = 0;
          }

          waitSleep((int)(SystemClock.elapsedRealtime() - t));
      }

      text_timestamp_buffer_ = null;
      rectangle_buffer_ = null;
  }

The real-time text watermark can be displayed or hidden by controlling:

public void enableText(boolean is_text) {
      is_text_ = is_text;
      clear_flag_ = true;
      if (handle_ != 0) {
          libPublisher.EnableLayer(handle_, timestamp_index_, is_text_?1:0);
          libPublisher.EnableLayer(handle_, text1_index_, is_text_?1:0);
          libPublisher.EnableLayer(handle_, text2_index_, is_text_?1:0);
      }
  }

To remove a layer, you can also call the RemoveLayer() interface. The specific design is as follows:

/**
   * 启用或者停用视频层, 这个接口必须在StartXXX之后调用.
   *
   * @param index: 层索引, 必须大于0, 注意第0层不能停用
   *
   * @param  is_enable: 是否启用, 0停用, 1启用
   *
   * @return {0} if successful
   */
  public native int EnableLayer(long handle, int index, int is_enable);


  /**
   * 移除视频层, 这个接口必须在StartXXX之后调用.
   *
   * @param index: 层索引, 必须大于0, 注意第0层不能移除
   *
   * @return {0} if successful
   */
  public native int RemoveLayer(long handle, int index);

For outer packaging such as startup watermark types:

private LayerPostThread layer_post_thread_ = null;

  private void startLayerPostThread() {
      if (3 == video_opt_) {
          if (null == layer_post_thread_) {
              layer_post_thread_ = new LayerPostThread();
              layer_post_thread_.startPost(publisherHandle, videoWidth, videoHeight, currentOrigentation, isHasTextWatermark(), isHasPictureWatermark());
          }
      }
  }

  private void stopLayerPostThread() {
      if (layer_post_thread_ != null) {
          layer_post_thread_.stopPost();
          layer_post_thread_ = null;
      }
  }

Summarize

The Android platform obtains the device power information and real-time signal status of the device, and then superimposes it on the video view and delivers it. For example, in scenarios such as law enforcement recorders, it is very valuable. It allows the GB28181 platform side to obtain more required information and scalability. extremely strong.

 

Guess you like

Origin blog.csdn.net/renhui1112/article/details/132073545