Tôi mới sử dụng JavaFX và Java. Tôi tự hỏi làm thế nào để tạo ra một cảnh trên một bề mặt của một khối lập phương hoặc bất kỳ polyhedra? Tôi muốn phát lại video trên bề mặt của bất kỳ hình dạng 3D nào. Làm thế nào tôi có thể đi về việc này?Tôi làm cách nào để sử dụng JavaFX để phát video trên hình dạng 3D?
Trả lời
Đây là một mẫu trong đó sử dụng trong xây dựng JavaFX MediaPlayer và chụp nhanh tuần hoàn của một cái nhìn trên phương tiện truyền thông thành một hình ảnh kết cấu ánh xạ vào một hình dạng 3D (trong trường hợp này một hộp). Một hình động xoay quanh trục Y được thêm vào để các cạnh của hộp có thể được nhìn thấy trong phối cảnh.
import javafx.animation.*;
import javafx.application.*;
import javafx.geometry.Rectangle2D;
import javafx.scene.*;
import javafx.scene.image.WritableImage;
import javafx.scene.media.*;
import javafx.scene.paint.*;
import javafx.scene.shape.*;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;
// Display a rotating 3D box with a video projected onto its surface.
public class ThreeDMedia extends Application {
private static final String MEDIA_URL =
"http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv";
private static final int SCENE_W = 640;
private static final int SCENE_H = 400;
private static final double MEDIA_W = 540 * 2/3;
private static final double MEDIA_H = 209 * 2/3;
private static final Color INDIA_INK = Color.rgb(35, 39, 50);
@Override
public void start(Stage stage) {
// create a 3D box shape on which to project the video.
Box box = new Box(MEDIA_W, MEDIA_H, MEDIA_W);
box.setTranslateX(SCENE_W/2);
box.setTranslateY(SCENE_H/2);
// create a media player for the video which loops the video forever.
MediaPlayer player = new MediaPlayer(new Media(MEDIA_URL));
player.setCycleCount(MediaPlayer.INDEFINITE);
// create a media view for the video, sized to our specifications.
MediaView mediaView = new MediaView(player);
mediaView.setPreserveRatio(false);
mediaView.setFitWidth(MEDIA_W);
mediaView.setFitHeight(MEDIA_H);
// project the video on to the 3D box.
showMediaOnShape3D(box, mediaView);
// rotate the box.
rotateAroundYAxis(box);
// create a point light source a fair way away so lighting is reasonably even.
PointLight pointLight = new PointLight(
Color.WHITE
);
pointLight.setTranslateX(SCENE_W/2);
pointLight.setTranslateY(SCENE_H/2);
pointLight.setTranslateZ(-SCENE_W * 5);
// add a bit of ambient light to make the lighting more natural.
AmbientLight ambientLight = new AmbientLight(
Color.rgb(15, 15, 15)
);
// place the shape and associated lights in a group.
Group group = new Group(
box,
pointLight,
ambientLight
);
// create a 3D scene with a default perspective camera.
Scene scene = new Scene(
group,
SCENE_W, SCENE_H, true, SceneAntialiasing.BALANCED
);
scene.setFill(INDIA_INK);
PerspectiveCamera camera = new PerspectiveCamera();
scene.setCamera(camera);
stage.setScene(scene);
stage.setResizable(false);
// start playing the media, showing the scene once the media is ready to play.
player.setOnReady(stage::show);
player.setOnError(Platform::exit);
player.play();
}
// Project video on to 3D shape.
private void showMediaOnShape3D(Shape3D shape3D, final MediaView mediaView) {
PhongMaterial material = new PhongMaterial();
shape3D.setMaterial(material);
Scene mediaScene = new Scene(
new Group(mediaView),
MEDIA_W, MEDIA_H
);
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setViewport(
new Rectangle2D(
0, 0, MEDIA_W, MEDIA_H
)
);
WritableImage textureImage = mediaView.snapshot(
snapshotParameters,
null
);
material.setDiffuseMap(textureImage);
AnimationTimer timer = new AnimationTimer() {
@Override
public void handle(long now) {
mediaView.snapshot(
snapshotParameters,
textureImage
);
}
};
timer.start();
}
// Rotates a shape around the y axis indefinitely.
private void rotateAroundYAxis(Shape3D shape3D) {
RotateTransition rotateY = new RotateTransition(
Duration.seconds(10),
shape3D
);
rotateY.setAxis(Rotate.Y_AXIS);
rotateY.setFromAngle(360);
rotateY.setToAngle(0);
rotateY.setCycleCount(RotateTransition.INDEFINITE);
rotateY.setInterpolator(Interpolator.LINEAR);
rotateY.play();
}
public static void main(String[] args) {
launch(args);
}
}
Thực tế, bạn có thể phát video trên bất kỳ hình dạng 3D nào.
Có một dự án tuyệt vời để phát video từ @caprica được gọi là VLCJ: Một khung công tác Java cho trình phát phương tiện VLC.
Mặc dù dự án được thiết kế để hiển thị trong canvas AWT, tác giả đã thực hiện một số tests để hiển thị nó trong JavaFX Canvas
.
Dựa trên lớp JavaFX của mình, thật dễ dàng để hiển thị bộ đệm trên hình dạng 3D thay vì nút canvas 2D.
Cài đặt
Trước tiên, bạn cần phải cài đặt máy nghe nhạc video đầu tiên VLC.
Sau đó, bạn cần một số phụ thuộc: vlcj-3.6.0.jar, jna-3.5-2.jar & platform-3.5.2.jar và slfj4j-api.1.7.12.jar.
Ngoài ra, tôi sẽ sử dụng một số hình dạng 3D tùy chỉnh từ thư viện FXyz, mặc dù bạn có thể sử dụng các hình thức thông thường từ API, chẳng hạn như Box
.
Cơ sở
Bí quyết để làm cho video trên một hình dạng 3D đang sử dụng bản đồ khuếch tán vật chất của nó, mà phải mất một hình ảnh, và xác định kết cấu của nó.
Bạn có thể tìm thêm một số thông tin về số here hoặc here này.
Vì vậy, đối với mỗi khung có sẵn, chúng tôi sẽ tạo ra một hình ảnh mới và thiết lập nó như là bản đồ khuếch tán:
ByteBuffer byteBuffer = nativeBuffer.getByteBuffer(0, nativeBuffer.size());
BufferFormat bufferFormat = ((DefaultDirectMediaPlayer) mediaPlayerComponent.getMediaPlayer()).getBufferFormat();
WritableImage textureImage = new WritableImage(bufferFormat.getWidth(), bufferFormat.getHeight());
if (bufferFormat.getWidth() > 0 && bufferFormat.getHeight() > 0) {
textureImage.getPixelWriter().setPixels(0, 0, bufferFormat.getWidth(), bufferFormat.getHeight(), pixelFormat, byteBuffer, bufferFormat.getPitches()[0]);
// apply new frame as texture image to the 3D shape's material
material.setDiffuseMap(textureImage);
}
Một AnimationTimer
sẽ cho phép cập nhật các khung và kết cấu.
mẫu
Đây là một mẫu làm việc mà ám video trên một hình xuyến phân đoạn.
public class Video3D extends Application {
static {
// path to the VLC video player
System.setProperty("jna.library.path", "C:/Program Files/VideoLAN/VLC");
}
// http://download.blender.org/peach/bigbuckbunny_movies/
// (c) copyright 2008, Blender Foundation/www.bigbuckbunny.org
private static final String VIDEO_FILE = "C:\\BigBuckBunny_320x180.mp4";
private final DirectMediaPlayerComponent mediaPlayerComponent;
private final WritablePixelFormat<ByteBuffer> pixelFormat;
private final SegmentedTorusMesh torus = new SegmentedTorusMesh(50,40,12,3.2d,4.5d);
private final PhongMaterial material = new PhongMaterial(Color.WHEAT);
private double mousePosX, mousePosY;
private double mouseOldX, mouseOldY;
private final Rotate rotateX = new Rotate(-20, Rotate.X_AXIS);
private final Rotate rotateY = new Rotate(240, Rotate.Y_AXIS);
private final AnimationTimer timer;
public TestVLC(){
mediaPlayerComponent = new TestMediaPlayerComponent();
pixelFormat = PixelFormat.getByteBgraInstance();
timer = new AnimationTimer() {
@Override
public void handle(long now) {
renderFrame();
}
};
}
protected void startTimer() {
mediaPlayerComponent.getMediaPlayer().playMedia(VIDEO_FILE);
timer.start();
}
protected void stopTimer() {
mediaPlayerComponent.getMediaPlayer().stop();
timer.stop();
}
@Override
public void start(Stage primaryStage) {
torus.setCullFace(CullFace.NONE);
torus.setzOffset(1.4);
torus.setMaterial(material);
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.getTransforms().addAll (rotateX, rotateY, new Translate(0, 0, -30));
Group root3D = new Group(camera,torus);
SubScene subScene = new SubScene(root3D, 800, 600, true, SceneAntialiasing.BALANCED);
subScene.setFill(Color.AQUAMARINE);
subScene.setCamera(camera);
BorderPane pane = new BorderPane();
pane.setCenter(subScene);
Button play = new Button("Play");
play.setOnAction(e->startTimer());
Button stop = new Button("Stop");
stop.setOnAction(e->stopTimer());
ToolBar toolBar = new ToolBar(play, stop);
toolBar.setOrientation(Orientation.VERTICAL);
pane.setRight(toolBar);
pane.setPrefSize(600,400);
Scene scene = new Scene(pane);
scene.setOnMousePressed((MouseEvent me) -> {
mouseOldX = me.getSceneX();
mouseOldY = me.getSceneY();
});
scene.setOnMouseDragged((MouseEvent me) -> {
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
rotateX.setAngle(rotateX.getAngle()-(mousePosY - mouseOldY));
rotateY.setAngle(rotateY.getAngle()+(mousePosX - mouseOldX));
mouseOldX = mousePosX;
mouseOldY = mousePosY;
});
primaryStage.setScene(scene);
primaryStage.setTitle("Video - JavaFX 3D");
primaryStage.show();
}
@Override
public final void stop() throws Exception {
stopTimer();
mediaPlayerComponent.getMediaPlayer().stop();
mediaPlayerComponent.getMediaPlayer().release();
}
/**
* Implementation of a direct rendering media player component that renders
* the video to a JavaFX canvas.
* https://github.com/caprica/vlcj-javafx/blob/master/src/test/java/uk/co/caprica/vlcj/javafx/test/JavaFXDirectRenderingTest.java
*/
private class TestMediaPlayerComponent extends DirectMediaPlayerComponent {
public TestMediaPlayerComponent() {
super(new TestBufferFormatCallback());
}
}
/**
* Callback to get the buffer format to use for video playback.
*/
private class TestBufferFormatCallback implements BufferFormatCallback {
@Override
public BufferFormat getBufferFormat(int sourceWidth, int sourceHeight) {
final int width = sourceWidth;
final int height = sourceHeight;
Platform.runLater(() -> {
torus.setMajorRadius(width/100);
torus.setMinorRadius(height/40);
});
return new RV32BufferFormat(width, height);
}
}
protected final void renderFrame() {
Memory[] nativeBuffers = mediaPlayerComponent.getMediaPlayer().lock();
if (nativeBuffers != null) {
Memory nativeBuffer = nativeBuffers[0];
if (nativeBuffer != null) {
ByteBuffer byteBuffer = nativeBuffer.getByteBuffer(0, nativeBuffer.size());
BufferFormat bufferFormat = ((DefaultDirectMediaPlayer) mediaPlayerComponent.getMediaPlayer()).getBufferFormat();
WritableImage textureImage = new WritableImage(bufferFormat.getWidth(), bufferFormat.getHeight());
if (bufferFormat.getWidth() > 0 && bufferFormat.getHeight() > 0) {
textureImage.getPixelWriter().setPixels(0, 0, bufferFormat.getWidth(), bufferFormat.getHeight(), pixelFormat, byteBuffer, bufferFormat.getPitches()[0]);
material.setDiffuseMap(textureImage);
}
}
}
mediaPlayerComponent.getMediaPlayer().unlock();
}
public static void main(String[] args) {
launch(args);
}
}
Và đây chỉ là hai ảnh chụp nhanh về những gì bạn sẽ nhận được.
Ngoài jewelsea giải pháp thanh lịch hơn José của và bạn luôn có thể xác định vị trí một MediaView bằng tay trong 3d không gian đ. g. như 6 gương mặt của một khối lập phương:
public class VideoCubeDemo extends Application {
Random rnd = new Random();
// size of the cube
double size = 320;
@Override
public void start(Stage primaryStage) throws MalformedURLException {
// create media views
List<String> videoFiles = new ArrayList<>();
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
videoFiles.add(getClass().getResource("funny_cats_compilation_2012.mp4").toExternalForm());
// create faces for the cube
// original cube face code from http://www.javafxapps.in/tutorial/Creating-3D-Cube-in-javafx.html
MediaView r;
int videoIndex;
Group cube = new Group();
List<MediaView> cubeFaces = new ArrayList<>();
// back face
videoIndex = 0;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(-0.5 * size);
r.setTranslateY(-0.5 * size);
r.setTranslateZ(0.5 * size);
cubeFaces.add(r);
// bottom face
videoIndex = 1;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(-0.5 * size);
r.setTranslateY(0);
r.setRotationAxis(Rotate.X_AXIS);
r.setRotate(90);
cubeFaces.add(r);
// right face
videoIndex = 2;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(-1 * size);
r.setTranslateY(-0.5 * size);
r.setRotationAxis(Rotate.Y_AXIS);
r.setRotate(90);
cubeFaces.add(r);
// left face
videoIndex = 3;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(0);
r.setTranslateY(-0.5 * size);
r.setRotationAxis(Rotate.Y_AXIS);
r.setRotate(90);
cubeFaces.add(r);
// top face
videoIndex = 4;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(-0.5 * size);
r.setTranslateY(-1 * size);
r.setRotationAxis(Rotate.X_AXIS);
r.setRotate(90);
cubeFaces.add(r);
// front face
videoIndex = 5;
r = createMediaView(videoFiles.get(videoIndex));
r.setTranslateX(-0.5 * size);
r.setTranslateY(-0.5 * size);
r.setTranslateZ(-0.5 * size);
cubeFaces.add(r);
// create cube with all faces
cube.getChildren().addAll(cubeFaces);
// initial cube rotation
cube.getTransforms().addAll(new Rotate(45, Rotate.X_AXIS), new Rotate(45, Rotate.Y_AXIS));
// animate cube
Point3D rotateAxis = new Point3D(1,1,1); // rotate around X, Y and Z
Timeline animation = new Timeline();
animation.getKeyFrames().addAll(
new KeyFrame(Duration.ZERO, new KeyValue(cube.rotationAxisProperty(), rotateAxis), new KeyValue(cube.rotateProperty(), 0d)),
new KeyFrame(Duration.seconds(5), new KeyValue(cube.rotationAxisProperty(), rotateAxis), new KeyValue(cube.rotateProperty(), 360d))
);
animation.setCycleCount(Animation.INDEFINITE);
// add objects to scene
StackPane root = new StackPane();
root.getChildren().add(cube);
Scene scene = new Scene(root, 1600, 900, true, SceneAntialiasing.BALANCED);
scene.setFill(Color.BLACK);
scene.setCamera(new PerspectiveCamera());
primaryStage.setResizable(true);
primaryStage.setScene(scene);
primaryStage.show();
// play videos and animation
for(MediaView mediaPlayer: cubeFaces) {
mediaPlayer.getMediaPlayer().play();
}
animation.play();
}
private MediaView createMediaView(String path) {
Media media = new Media(path);
MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.setVolume(0.8);
mediaPlayer.setCycleCount(MediaPlayer.INDEFINITE);
MediaView mediaView = new MediaView(mediaPlayer);
mediaView.setFitHeight(size);
mediaView.setFitWidth(size);
mediaView.setPreserveRatio(false);
return mediaView;
}
public static void main(String[] args) {
launch(args);
}
}
- 1. Tôi nên sử dụng định dạng Mô hình 3D nào?
- 2. Tôi làm cách nào để phát video từ ServiceWorker?
- 3. Làm cách nào để phát hiện phim là 3D?
- 4. Làm cách nào để phát triển trình phát video nhận luồng video trên phát sóng/phát đa hướng wi-fi?
- 5. Làm cách nào để đưa mô hình 3ds Max của tôi vào trò chơi iPhone 3D?
- 6. Làm cách nào để phát trực tuyến video?
- 7. Làm cách nào để xuất hình ảnh 3D vào TV 3D?
- 8. Làm cách nào để chúng tôi có thể thêm hình ảnh động/đơn giản làm lớp trên cùng cho video và xuất dưới dạng một video trong Android?
- 9. Làm cách nào để lưu khung hình đầu tiên của video dưới dạng hình ảnh?
- 10. Tôi làm cách nào để tự động phát video bằng cách sử dụng kiểu mã nhúng mới cho Youtube?
- 11. JavaFX 3D Transparency
- 12. Làm cách nào để vẽ hình cầu 3D?
- 13. Tôi làm cách nào để tạo luồng video trực tiếp trên youtube?
- 14. Tôi làm cách nào để phát luồng trực tiếp Apple HLS bằng thẻ video html5
- 15. Làm cách nào để che phủ nút phát qua hình thu nhỏ video?
- 16. Làm cách nào để sử dụng giải mã video tăng tốc phần cứng trên Android?
- 17. Phát video trên TextureView
- 18. Tôi làm cách nào để thay đổi kích thước hình ảnh số lần xem trong javafx?
- 19. Tôi làm cách nào để xoay xem trước video libstreaming?
- 20. Có cách nào để chụp màn hình dưới dạng video theo thời gian thực không?
- 21. Tôi làm cách nào để phát video từ byte trong android
- 22. Cách xử lý hình ảnh của video, khung theo khung hình trong phát video bằng cách sử dụng python Opencv
- 23. Tôi làm cách nào để ẩn các video có liên quan ở cuối mã nhúng danh sách phát trên YouTube?
- 24. Làm cách nào để triển khai chức năng awt.CardLayout trong ứng dụng javaFX 2.0 của tôi?
- 25. Làm cách nào để tôi có thể tạo video nhúng của mình trên Kindle Fire?
- 26. Tôi làm cách nào để quay video trong trình duyệt?
- 27. Làm cách nào để tôi có thể phát các tệp video khác nhau trong ứng dụng Android?
- 28. Làm cách nào để nhúng trình phát video flash của riêng tôi vào Facebook?
- 29. Làm cách nào để hiển thị video trên YouTube dưới dạng kết cấu trong A-Frame?
- 30. Làm cách nào để nhúng trình phát video của riêng tôi vào Facebook?
Nó rất tuyệt vời để thấy rằng nó cũng làm việc khi bạn áp dụng nó trên một Sphere hoặc một xi lanh. Cảm ơn bạn đã chia sẻ điều đó! – Roland