Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在一行中获取长度未知的未声明变量的最后5位?_Java_Variables_Command Line_Screenshot_Bit Manipulation - Fatal编程技术网

Java 在一行中获取长度未知的未声明变量的最后5位?

Java 在一行中获取长度未知的未声明变量的最后5位?,java,variables,command-line,screenshot,bit-manipulation,Java,Variables,Command Line,Screenshot,Bit Manipulation,更新: 有一些很好的解决方案,但我意识到在我的前提下有一个问题:似乎rt.exec在流程执行后不会返回到java代码。谢谢你的帮助 我想以编程的方式每隔一秒获取一个screencap,每个screencap的标题应该是时间戳。我用vlc来实现这一点 starttime = System.nanoTime(); screencapProcess = rt.exec("C:\\VLC\\vlc screen:// --dshow-vdev=screen-capture-record

更新:

有一些很好的解决方案,但我意识到在我的前提下有一个问题:似乎rt.exec在流程执行后不会返回到java代码。谢谢你的帮助


我想以编程的方式每隔一秒获取一个screencap,每个screencap的标题应该是时间戳。我用vlc来实现这一点

    starttime = System.nanoTime();
    screencapProcess = rt.exec("C:\\VLC\\vlc screen:// --dshow-vdev=screen-capture-recorder --dshow-fps=1 -I dummy --dummy-quiet --rate=1 --video-filter=scene --vout=dummy --scene-format=jpg --scene-ratio=1 --scene-prefix=snap --scene-path=" + path +" --scene-prefix="+ "ScreenCapAt" +   ( String.valueOf(((long)System.nanoTime() - starttime)).substring(9, 14))+" vlc://quit ");
有关部分是:

    String.valueOf(((long)System.nanoTime() - starttime)).substring(9, 14))
这会导致小于1的纳米时间出现索引越界异常

我不能将“System.nanoTime()-starttime”声明为此行之外的变量来获取其长度,因为这会改变我的时间

有没有办法在一行中获取这个长度未知的未声明变量的最后5位数字

这个答案需要符合我的rt.exec行

一些想法:

  • 按位移位和掩蔽
  • 管道进入我的执行
  • 后处理
  • 创建新类
  • 再次调用System.nanoTime()-starttime将增加操作时间并中断我的计算
  • 有没有更好的方法从原子上获得捕获时间
试试这个:

 String diff=""+ ((long)System.nanoTime() - starttime);

 if(diff.length() >4){// Check if diff length is less than 5
   String lastFive=diff.substring(diff.length()-5, diff.length());
 }
尝试:

更新

如果你想重新计算这个值,我建议你把它放到一个单独的函数中

    public static void main(String[] args) throws InterruptedException{ 

    long starttime = (long)System.nanoTime(); 
    Thread.sleep(1000); 
    System.out.println("test1 " + getLastFive (starttime)); 
    Thread.sleep(500); 
    System.out.println("test2 " + getLastFive (starttime)); 
    Thread.sleep(1000);
} 

static private String getLastFive (long starttime) {

    String diff=""+ ((long)System.nanoTime() - starttime); 
    String lastFive= diff.length() > 4 ? diff.substring(diff.length()-5, diff.length()) : diff; 
    return lastFive;

}

我想你必须在不止一条线上做这件事。我看不出一种不计算字符串长度的方法。这是我能想到的最好的办法:

String diff = ""+((long)System.nanoTime() - starttime);
String lastfive = diff.substring(Math.max(0, diff.length() - 5));
更新:找到了一种在一行中完成所有操作的方法:

String lastFive = (""+((long)System.nanoTime() - starttime)).replaceAll("(.*)(\\d{5}$)","$2");
在1行中(计算声明除外):



你为什么在截图中使用VLC,这是一个要求吗???使用VLC不是一个要求,但是能够改变截图的fps是一个要求。您会推荐一个不同的实用程序吗?是否有任何特殊原因需要在一行中执行此操作?如果System.nanoTime()和starttime之间的差异小于10000,则此操作将失败,我猜这是他在第一次迭代时遇到的问题。这在我的rt.exec行中不起作用,因为它不止一行。这在我的rt.exec行中不起作用,因为它不止一行。更新了我的答案,使用正则表达式只保留最后5个字符。干得好!虽然我意识到我拍摄电影的方式有问题,但无论如何我都要重写。出于好奇,我将如何获得前5个?翻转正则表达式轮并获得第一个匹配,而不是第二个:
…replaceAll((\\d{5})(.*),“$1”)
这在我的rt.exec行中不起作用,因为它不止一行。请在rt.exec行中使用变量lastfive。看我的最新答案这不是给我每个屏幕盖相同的名字吗?rt.exec应该给我30个左右不同名称的屏幕封口。每次调用上述代码块时,
lastfive
将被重新计算,然后传递到rt.exec。为什么它必须在一条线上?它不会被重新评估。publicstaticvoidmain(String[]args)抛出中断异常{long starttime=(long)System.nanoTime();float calc;Thread.sleep(1000);String diff=“”+((long)System.nanoTime()-starttime);String lastFive=diff.length()>4?diff.substring(diff.length()-5,diff.length()):diff;System.out.println(“test1”+lastFive);Thread.sleep(500);System.out.println(“test2”+lastFive);Thread.sleep(1000);}输出:test1 81090 test2 81090这看起来不错!我得到了81E8这样的答案,但这可以通过转换为秒来解决。当我进一步使用它时,我会评论一个更适合我需要的版本。另外,别忘了,我使用的是float calc;并且渴望System.nanoTime()根据需要更改这些类型。也不要忘记检查它是否大于4个字符。这也可以在一行中完成。我通过更改Math.max.System.out.println(“test3”+String.valueOf((calc=(float)((long)System.nanoTime()-starttime)/100000000.00))的第一个参数进行检查。子字符串(Math.max)(4,String.valueOf(calc.length()-5));工作正常。但当我在rt.exec中运行它时,我会得到不同的结果,比如ScreenCapAt0.00138500001.jpg。我现在正在尝试一种混合解决方案,我将该行放在函数中,并在rt.exec行中调用它。这并不理想(肯定不是一行),但让我们做点工作并从那里开始:)好吧,但是这个结果有什么不对劲吗?应该是怎样的?
String lastFive = (""+((long)System.nanoTime() - starttime)).replaceAll("(.*)(\\d{5}$)","$2");
float calc;
String diff = String.valueOf((calc = (long)System.nanoTime() - starttime)).substring(Math.max(0, String.valueOf(calc).length() - 5));