简体   繁体   中英

how to load a 3D model on camera?

There are many libraries to load a 3D model inside an activity. I'm trying to load a 3D model on a live camera. but I don't find anything to do this.

one the popular library is Rajawali :

https://github.com/Rajawali/Rajawali

but I don't know is it possible to load a 3D model on live camera or not.

I used google's ARCore but I think I should be waiting to release second version.

my questions are:

  1. Is it possible to load a 3D model on live camera?
  2. how to load a 3D model on live camera with Rajawali library.

My solution was to place an org.rajawali3d.SurfaceView with transparent background over an android.view.TextureView that displays the hardware camera:

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent">

    <TextureView
        android:id="@+id/camera_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- org.rajawali3d.view. -->

    <org.rajawali3d.view.SurfaceView
        android:id="@+id/rajawali_surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent"/>
</FrameLayout>

And here is MainActivity.java

import android.app.Activity;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.TextureView;

import org.rajawali3d.view.ISurface;
import org.rajawali3d.view.SurfaceView;

import java.io.IOException;

import eiggsplore.scot.eiggsplore2.world3d.TerrainRenderer;

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener{

    private TerrainRenderer renderer;
    private Camera cam;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SurfaceView rajawali_surface = (SurfaceView)findViewById(R.id.rajawali_surface);
        rajawali_surface.setFrameRate(60.0);
        rajawali_surface.setRenderMode(ISurface.RENDERMODE_WHEN_DIRTY);
        rajawali_surface.setTransparent(true);
        renderer = new TerrainRenderer(rajawali_surface, this);


        TextureView camera_surface =(TextureView)findViewById(R.id.camera_surface);
        camera_surface.setSurfaceTextureListener(this);
    }

    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        cam = Camera.open();

        try {
            cam.setPreviewTexture(surface);
            cam.startPreview();
        } catch (IOException ioe) {
            // Something bad happened
        }
    }

    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, Camera does all the work for us
    }

    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        cam.stopPreview();
        cam.release();
        return true;
    }

    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Invoked every time there's a new Camera preview frame
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM