我在CameraView中显示TextureView,并且正在使用TextureView.getBitmap()TextureView获取位图。

每次我呼叫TextureView.getBitmap()都会导致Camera预览冻结一纳秒。我想在后台运行此任务。

谢谢

这是我尝试过的代码:

     findViewById(R.id.capture).setOnClickListener(new OnClickListener() {
           @Override
           public void onClick(View v) {
             Bitmap bmp =  mTextureView.getBitmap();
             rev = new ArrayList<Bitmap>();
             rev.add(bmp);
             Toast.makeText(getApplicationContext(),""+ rev.size(), Toast.LENGTH_LONG).show();
           }
        });


这是我的Logcat

       08-22 03:59:57.867: E/AndroidRuntime(4859): FATAL EXCEPTION: main
        08-22 03:59:57.867: E/AndroidRuntime(4859): java.lang.NullPointerException
        08-22 03:59:57.867: E/AndroidRuntime(4859):     at                              com.example.hangout_gif_camera.MainActivity$1.onClick(MainActivity.java:41)
      08-22 03:59:57.867: E/AndroidRuntime(4859):   at            android.view.View.performClick(View.java:4211)
     08-22 03:59:57.867: E/AndroidRuntime(4859):    at       android.view.View$PerformClick.run(View.java:17446)
     08-22 03:59:57.867: E/AndroidRuntime(4859):    at   android.os.Handler.handleCallback(Handler.java:725)
    08-22 03:59:57.867: E/AndroidRuntime(4859):     at         android.os.Handler.dispatchMessage(Handler.java:92)
    08-22 03:59:57.867: E/AndroidRuntime(4859):     at android.os.Looper.loop(Looper.java:153)
     08-22 03:59:57.867: E/AndroidRuntime(4859):    at android.app.ActivityThread.main(ActivityThread.java:5297)
   08-22 03:59:57.867: E/AndroidRuntime(4859):  at java.lang.reflect.Method.invokeNative(Native Method)
    08-22 03:59:57.867: E/AndroidRuntime(4859):     at java.lang.reflect.Method.invoke(Method.java:511)
    08-22 03:59:57.867: E/AndroidRuntime(4859):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
    08-22 03:59:57.867: E/AndroidRuntime(4859):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
      08-22 03:59:57.867: E/AndroidRuntime(4859):   at dalvik.system.NativeStart.main(Native Method)

最佳答案

 freeze for a nano second

您甚至不会意识到它冻结了我们的眼睛,无法做出那么快的反应(除非您是超人)。
但是,如果您真的希望它在单独的线程中运行,则可以尝试以下操作:
编辑:
findViewById(R.id.capture).setOnClickListener(new OnClickListener() {
               @Override
               public void onClick(View v) {
                 Thread td = new Thread(new Runnable() {
                   public void run()
                   {
                         Bitmap bmp =  mTextureView.getBitmap();
                         rev = new ArrayList<Bitmap>();
                         rev.add(bmp);
                         runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                 Toast.makeText(getApplicationContext(),""+ rev.size(), Toast.LENGTH_LONG).show();
                            }
                        });
                   }
                 });
                 td.start(); //start the thread

               }
            });

关于java - TextureView:在后台执行TextureView.getBitmap(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25438814/

10-10 23:31