【问题标题】:How to set the name of a snapshot file如何设置快照文件的名称
【发布时间】:2014-06-04 02:35:11
【问题描述】:

我正在开发一个在我的 W595s 索尼爱立信手机上运行的 J2ME 应用程序。
我的应用程序使用 JSR 135 Mobile Media API 和 JSR 234 Advanced Multimedia Supplements API。

我的应用程序显示一个表单。
摄像头视频显示在表单的项目中。
表单有一个命令。
当用户激活命令时,应用程序会拍摄快照。
快照文件保存在记忆棒的图片目录中。

这是表单的 commandAction 事件监听器:

public void commandAction(Command arg0, Displayable arg1) {

    m_snapshotControl.setDirectory("e:/Picture");
    m_snapshotControl.setFilePrefix("AC");
    m_snapshotControl.setFileSuffix(".JPG");

    int[]resolutions = m_cameraControl.getSupportedStillResolutions();
    int maxValue = (resolutions.length / 2) - 1;
    m_cameraControl.setStillResolution(maxValue);

    m_snapshotControl.start(1);
}

我运行了我的应用程序 2 次。
在第一次运行之前,图片目录不包含任何快照文件。
我在每次运行期间都做了以下操作:

  1. 我激活了命令
  2. 我对以下权限请求对话框的回答是:
    1. 允许应用程序读取用户数据?
    2. 允许应用程序写入用户数据?
    3. 允许应用程序读取用户数据?
    4. 允许应用程序写入用户数据?
    5. 允许应用使用相机拍摄?
    6. 允许应用程序读取用户数据?
    7. 允许应用程序写入用户数据?

AC0000.jpg 快照文件是在第一次运行后创建的。
第二次运行后替换了AC0000.jpg图片文件。

我不希望我的应用程序替换过去运行期间拍摄的快照。
如何在拍摄快照之前设置快照文件的名称?
是否可以在前缀和后缀之间设置字符串?

任何帮助将不胜感激

【问题讨论】:

    标签: camera java-me snapshot


    【解决方案1】:

    我还没有尝试过这个特定范围的手机,但是这个怎么样:

    private int iSnapshotCounter = 0;
    
    public void commandAction(Command arg0, Displayable arg1) {
    
        m_snapshotControl.setDirectory("e:/Picture");
        m_snapshotControl.setFilePrefix("AC");
        m_snapshotControl.setFileSuffix( (++iSnapshotCounter) + ".JPG");
    
        int[]resolutions = m_cameraControl.getSupportedStillResolutions();
        int maxValue = (resolutions.length / 2) - 1;
        m_cameraControl.setStillResolution(maxValue);
    
        m_snapshotControl.start(1);
    }
    

    如果可行,您可以决定在文件名中包含日期和时间(比如秒)。

    当然,如果 setFileSuffix() 不允许您指定多个文件扩展名,您可以尝试在前缀字符串上使用相同的技巧。

    您可能还需要使用 JSR-75 来确定文件夹中已经存在哪些文件。

    【讨论】:

      【解决方案2】:

      我已将 PlayerListener 添加到 SnapshotControl 的 Player。
      当 SHOOTING_STOPPED 事件发生时,我的 playerUpdate 方法会重命名创建的快照文件。
      新名称由当前日期和时间的部分组成。
      新名称的格式为 YYYYMMDD_HHMMSS.jpg。
      这是 playerUpdate 方法:

      public void playerUpdate(Player arg0, String arg1, Object arg2) {
      
          if (arg1.equalsIgnoreCase(SnapshotControl.SHOOTING_STOPPED)) {
      
              FileConnection fconn = null;
              try {
                  fconn = (FileConnection)Connector.open("file:///e:/Picture/" + (String)arg2);
      
                  Date now = new Date();
                  Calendar calendar = Calendar.getInstance();
                  calendar.setTime(now);
      
                  StringBuffer buffer = new StringBuffer();
                  buffer.append(calendar.get(Calendar.YEAR));
      
                  int month = calendar.get(Calendar.MONTH);
                  month++;
                  if (month < 10) {
                      buffer.append(0);
                  }
                  buffer.append(month);
      
                  int day = calendar.get(Calendar.DAY_OF_MONTH);
                  if (day < 10) {
                      buffer.append(0);
                  }
                  buffer.append(day);
      
                  buffer.append("_");
      
                  int hour = calendar.get(Calendar.HOUR_OF_DAY);
                  if (hour < 10) {
                      buffer.append(0);
                  }
                  buffer.append(hour);
      
                  int minute = calendar.get(Calendar.MINUTE);
                  if (minute < 10) {
                      buffer.append(0);
                  }
                  buffer.append(minute);
      
                  int second = calendar.get(Calendar.SECOND);
                  if (second < 10) {
                      buffer.append(0);
                  }
                  buffer.append(second);
      
                  buffer.append(".jpg");
      
                  fconn.rename(buffer.toString());
      
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              } finally {
                  try {
                      fconn.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2011-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-07-05
        相关资源
        最近更新 更多