T
T
titovnikita2014-05-05 00:23:34
Android
titovnikita, 2014-05-05 00:23:34

Doesn't draw on SurfaceView, android?

Hello, I need your help.
To solve my problem, I need 2 SurfaceViews: 1 to take and process frames from the camera, the second to draw the received data. So, when a Canvas is requested, the lockCanvas function returns null.

private SurfaceView sv;
  private SurfaceView svfd; // Surface view for drawing
  private SurfaceHolder holder;
  private SurfaceHolder holderFD; // Holder for drawing on Surface View
  private HolderCallback holderCallback;
  private Camera camera;
  ...
  private Paint paint;
  
  private final PreviewCallback previewCallback;
  
  final int CAMERA_ID = 0;
  final boolean FULL_SCREEN = true;
  private int ocr_count = 0; // TODO Delete after tests
  ...

  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      Intent intent = getIntent();
      mLang = intent.getStringExtra("lang");
      requestWindowFeature(Window.FEATURE_NO_TITLE);
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
          WindowManager.LayoutParams.FLAG_FULLSCREEN);
      setContentView(R.layout.realtime);

      sv = (SurfaceView) findViewById(R.id.surfaceView);
      svfd = (SurfaceView) findViewById(R.id.surfaceViewForDrawing);
      svfd.setZOrderOnTop(true);
      holder = sv.getHolder();
      holderFD = svfd.getHolder();

      holderCallback = new HolderCallback(this, this);
      holder.addCallback(holderCallback);
      
      handler = new RealTimeHandler(this, camera, mLang);
      previewCallback.setHandler(handler);
      
      paint = new Paint();
    }
...

 public void drawFounded(OcrDB OcrResult){
      if(OcrResult!=null){
        Canvas canvas = holderFD.lockCanvas(); // ВОТ ТУТ ВОЗВРАЩАЕТСЯ null
        if (canvas == null) {
          Log.e("Recognized", "Cannot draw onto the canvas as it's null");
        } else { 
          OcrResult.getWords(); 
          OcrResult.getFoundedWordsArray("your"); // input a word to find
          Log.e("Recognized", "else of drawFounded starting");
              
          if(OcrResult.getFoundedWordNum().size()>0){
          // Draw bounding boxes around each word
          for (int i = 0; i < OcrResult.getFoundedWordNum().size(); i++) {
            paint.setAlpha(0xFF);
            paint.setColor(0xFF00CCFF);
            paint.setStyle(Style.STROKE);
            paint.setStrokeWidth(2);
            Rect r = OcrResult.getWordBoundingBoxes().get(OcrResult.getFoundedWordNum().get(i));
            canvas.drawRect(r, paint);
            Toast.makeText(this, "founded", Toast.LENGTH_SHORT).show();
            Log.e("Recognized", "trying to draw");
          }
          holderFD.unlockCanvasAndPost(canvas);
          Log.e("Recognized", "unlock canvas");
          //sv.setZOrderOnTop(true);
          //svfd.setZOrderOnTop(false);  // try another combination
          }
          }
      }
    }

Thank you in advance for your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
titovnikita, 2014-05-13
@titovnikita

Yes, everything is there. I solved the problem by replacing the SurfaceView with my custom View. Thanks

S
StanKo, 2014-05-12
@StanKo

According to the description , first Callback.surfaceCreated must work in the obligatory - is there such a thing? because you can't see it in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question