2017-10-12 11 views
5

Tôi đang cố gắng sử dụng phát hiện dòng BoofCV với ví dụ cụ thể từ the BoofCV Android Demo. Đối với điều này tôi đã sao chép các lớp và thiết lập mọi thứ với API Camera từ Android. Mặc dù Demo đang sử dụng Định hướng ngang, Hoạt động của tôi cần phải ở chế độ Chân dung, nhưng khi đặt máy ảnh được xoay 90 ° sang trái. Khi tôi cố gắng đặt Camera cho phù hợp, không có gì xảy ra. Tôi đã sử dụng:Tại sao BoofCV liên tục xoay Xem trước Máy ảnh ở bên trái?

  • Camera.setDisplayOrientation(90)
  • Camera.setParameters("orientation", "portrait")

Sau một thời gian tôi đã tìm ra rằng nó không phải là thiết bị liên quan (thử nghiệm trên các thiết bị khác nhau và các cấp API) và nó không liên quan gì đến với API Camera nữa (vì tôi đã quản lý nó ở chế độ chân dung khi nhận xét chức năng VideoProcessor.init()).

Sau khi thử nó trong một thời gian tôi vẫn không thể hiểu tại sao các VideoProcessor giữ xoay hình ảnh sang trái ...

Đây là mã của tôi cho VideoProcessor:

public class LineProcessor extends Thread implements VideoProcessing { 

    /** 
    * Lock for reading and writing images with processing and render 
    */ 
    private final Object lockGui = new Object(); 

    /** 
    * Lock used when converting the video stream. 
    */ 
    private final Object lockConvert = new Object(); 

    private Paint mPaint; 
    private ImageType<GrayU8> imageType; 
    private GrayU8 image; 
    private GrayU8 image2; 
    private volatile boolean requestStop = false; 
    private volatile boolean running = false; 
    private int outputWidth; 
    private int outputHeight; 
    private View view; 
    private Thread thread; 

    private DetectLine detector; 
    private FastQueue<LineSegment2D_F32> lines = new FastQueue<LineSegment2D_F32>(LineSegment2D_F32.class,true); 
    private Bitmap bitmap; 
    private byte[] storage; 

    private double scale; 
    private double tranX,tranY; 

    /** 
    * Creates a new Line Processor from a Line Detector 
    * @param detector the Line Detector Segment 
    */ 
    public LineProcessor(DetectLine detector) { 
     this.imageType = ImageType.single(GrayU8.class); 
     this.detector = detector; 

     mPaint = new Paint(); 
     mPaint.setColor(Color.RED); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeWidth(2.0f); 
    } 

    @Override 
    public void init(View view, Camera camera) { 
     synchronized (lockGui) { 
      this.view = view; 

      Camera.Size size = camera.getParameters().getPreviewSize(); 
      outputWidth = size.width; 
      outputHeight = size.height; 
      declareImages(size.width,size.height); 
     } 

     // start the thread for processing 
     running = true; 
     start(); 
    } 



    @Override 
    public void onDraw(Canvas canvas) { 
     synchronized (lockGui) { 
      // the process class could have been swapped 
      if(image == null) 
       return; 

      int w = view.getWidth(); 
      int h = view.getHeight(); 

      // fill the window and center it 
      double scaleX = w/(double)outputWidth; 
      double scaleY = h/(double)outputHeight; 

      scale = Math.min(scaleX,scaleY); 
      tranX = (w-scale*outputWidth)/2; 
      tranY = (h-scale*outputHeight)/2; 

      canvas.translate((float)tranX,(float)tranY); 
      canvas.scale((float)scale,(float)scale); 

      render(canvas, scale); 
     } 
    } 



    @Override 
    public void convertPreview(byte[] bytes, Camera camera) { 
     if(thread == null) 
      return; 

     synchronized (lockConvert) { 
      ConvertUtils.nv21ToGray(bytes, image.width, image.height, image); 

     } 
     // wake up the thread and tell it to do some processing 
     thread.interrupt(); 
    } 

    @Override 
    public void stopProcessing() { 
     if(thread == null) 
      return; 

     requestStop = true; 
     while(running) { 
      // wake the thread up if needed 
      thread.interrupt(); 
      try { 
       Thread.sleep(10); 
      } catch (InterruptedException e) {} 
     } 
    } 

    @Override 
    public void run() { 
     thread = Thread.currentThread(); 
     while(!requestStop) { 
      synchronized (thread) { 
       try { 
        wait(); 
        if(requestStop) 
         break; 
       } catch (InterruptedException e) {} 
      } 

      // swap gray buffers so that convertPreview is modifying the copy which is not in use 
      synchronized (lockConvert) { 
       GrayU8 tmp = image; 
       image = image2; 
       image2 = tmp; 
      } 

      process(image2); 

      view.postInvalidate(); 
     } 
     running = false; 
    } 

    /** 
    * Scaling applied to the drawing canvas 
    */ 
    public double getScale() { 
     return scale; 
    } 

    /** 
    * Translation x applied to the drawing canvas 
    */ 
    public double getTranX() { 
     return tranX; 
    } 

    /** 
    * Translation y applied to the drawing canvas 
    */ 
    public double getTranY() { 
     return tranY; 
    } 


    public void process(GrayU8 gray) { 

     if(detector != null) { 
      List<LineParametric2D_F32> found = detector.detect(gray); 

      synchronized (lockGui) { 
       ConvertUtils.grayToBitmap(gray,bitmap,storage); 
       lines.reset(); 
       for(LineParametric2D_F32 p : found) { 
        LineSegment2D_F32 ls = ConvertUtils.convert(p, gray.width,gray.height); 
        lines.grow().set(ls.a,ls.b); 
       } 
      } 

     } 
    } 

    protected void render(Canvas canvas, double imageToOutput) { 

     canvas.drawBitmap(bitmap,0,0,null); 

     for(LineSegment2D_F32 s : lines.toList()) { 
      canvas.drawLine(s.a.x,s.a.y,s.b.x,s.b.y,mPaint); 
     } 

} 

    protected void declareImages(int width , int height) { 
     image = imageType.createImage(width, height); 
     image2 = imageType.createImage(width, height); 

     bitmap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); 
     storage = ConvertUtils.declareStorage(bitmap,storage); 
    } 
} 

sự lớp tôi mở rộng là VideoProcessing.java

Có ai có bất kỳ kinh nghiệm nào về vấn đề này không?

+3

Bạn có thử xoay canvas dựa trên xoay máy ảnh? – noongiya95

+0

Vâng, nó thực sự là giải pháp cho vấn đề ... BoofCV rút ra công cụ của nó trên một canvas mới và vì bất cứ lý do gì quay nó .... Tôi đã thay đổi chức năng xử lý thành những gì im sẽ đăng trong câu hỏi ban đầu và nó hoạt động hoàn hảo khỏe! Cảm ơn rất nhiều! – 1resu

Trả lời

2

Các giải pháp được thay đổi làm cho chức năng như sau:

protected void render(Canvas canvas, double imageToOutput) { 

     canvas.rotate(90, 640f/2, 480f/2); 
     canvas.scale(480f/640f, 640f/480f, 640f/2, 480f/2); 
     canvas.drawBitmap(bitmap,0,0,null); 

     for(LineSegment2D_F32 s : lines.toList()) { 
      canvas.drawLine(s.a.x,s.a.y,s.b.x,s.b.y,mPaint); 
     } 
} 

Tôi nghĩ đây không phải là cách làm sạch trước đó, nhưng nó thực sự là cách duy nhất làm việc ....

Các vấn đề liên quan