Android Open Source - AndroidSensorApp Native Camera Interface






From Project

Back to project page AndroidSensorApp.

License

The source code is released under:

Apache License

If you think the Android project AndroidSensorApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package arpg.nativesensorinterface;
/* ww  w. j av a2 s .co m*/
import java.io.IOException;

import android.content.Context;
import android.hardware.Camera;
import android.graphics.SurfaceTexture;
import android.os.SystemClock;
import android.view.SurfaceHolder;
import android.view.TextureView;
import android.util.Log;

public class NativeCameraInterface
    implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private NativeSensorInterface mNativeInterface;
    private TextureView mTextureView;
    private long mTimestamp;
    private boolean mHasInitialImage;
    private long mInitialTimestamp, mRealImageTime;

    public NativeCameraInterface(NativeSensorInterface nativeInterface,
                                 TextureView textureView) {
        mHasInitialImage = false;
        mTimestamp = 0;
        mNativeInterface = nativeInterface;
        mTextureView = textureView;
        mTextureView.setSurfaceTextureListener(this);

        mCamera = Camera.open();
        mCamera.setPreviewCallback(new Camera.PreviewCallback() {
                @Override
                public void onPreviewFrame(byte[] data, Camera camera) {
                    mNativeInterface.postImage(mTimestamp, data);
                }
            });
    }

    public void stop() {
        mCamera.stopPreview();
    }

    public void close() {
        mCamera.release();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface,
                                          int width, int height) {
        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException ioe) {
            // Something bad happened
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface,
                                            int width, int height) {}

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        if (!mHasInitialImage) {
            mHasInitialImage = true;
            mInitialTimestamp = surface.getTimestamp();
            mRealImageTime = SystemClock.elapsedRealtimeNanos();
        }
        mTimestamp =
            surface.getTimestamp() - mInitialTimestamp + mRealImageTime;
    }
}




Java Source Code List

arpg.nativesensorinterface.MainActivity.java
arpg.nativesensorinterface.NativeCameraInterface.java
arpg.nativesensorinterface.NativeOpenGLRenderer.java
arpg.nativesensorinterface.NativeSensorInterface.java