-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
TeamCode/src/main/java/org/firstinspires/ftc/teamcode/VisionPortalStreamingOpMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.firstinspires.ftc.teamcode; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
|
||
import com.acmerobotics.dashboard.FtcDashboard; | ||
import com.qualcomm.robotcore.eventloop.opmode.Autonomous; | ||
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; | ||
|
||
import org.firstinspires.ftc.robotcore.external.function.Consumer; | ||
import org.firstinspires.ftc.robotcore.external.function.Continuation; | ||
import org.firstinspires.ftc.robotcore.external.hardware.camera.BuiltinCameraDirection; | ||
import org.firstinspires.ftc.robotcore.external.stream.CameraStreamSource; | ||
import org.firstinspires.ftc.robotcore.internal.camera.calibration.CameraCalibration; | ||
import org.firstinspires.ftc.vision.VisionPortal; | ||
import org.firstinspires.ftc.vision.VisionProcessor; | ||
import org.opencv.android.Utils; | ||
import org.opencv.core.Mat; | ||
|
||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
@Autonomous | ||
public class VisionPortalStreamingOpMode extends LinearOpMode { | ||
public static class CameraStreamProcessor implements VisionProcessor, CameraStreamSource { | ||
private final AtomicReference<Bitmap> lastFrame = new AtomicReference<>(Bitmap.createBitmap(1, 1, Bitmap.Config.RGB_565)); | ||
|
||
@Override | ||
public void init(int width, int height, CameraCalibration calibration) { | ||
lastFrame.set(Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)); | ||
} | ||
|
||
@Override | ||
public Object processFrame(Mat frame, long captureTimeNanos) { | ||
Bitmap b = Bitmap.createBitmap(frame.width(), frame.height(), Bitmap.Config.RGB_565); | ||
Utils.matToBitmap(frame, b); | ||
lastFrame.set(b); | ||
return null; | ||
} | ||
|
||
@Override | ||
public void onDrawFrame(Canvas canvas, int onscreenWidth, int onscreenHeight, float scaleBmpPxToCanvasPx, float scaleCanvasDensity, Object userContext) { | ||
// do nothing | ||
} | ||
|
||
@Override | ||
public void getFrameBitmap(Continuation<? extends Consumer<Bitmap>> continuation) { | ||
continuation.dispatch(bitmapConsumer -> bitmapConsumer.accept(lastFrame.get())); | ||
} | ||
} | ||
|
||
@Override | ||
public void runOpMode() throws InterruptedException { | ||
final CameraStreamProcessor processor = new CameraStreamProcessor(); | ||
|
||
new VisionPortal.Builder() | ||
.addProcessor(processor) | ||
.setCamera(BuiltinCameraDirection.BACK) | ||
.build(); | ||
|
||
FtcDashboard.getInstance().startCameraStream(processor, 0); | ||
|
||
waitForStart(); | ||
|
||
while (opModeIsActive()) { | ||
sleep(100L); | ||
} | ||
} | ||
} |