Example usage for android.view SurfaceView getHolder

List of usage examples for android.view SurfaceView getHolder

Introduction

In this page you can find the example usage for android.view SurfaceView getHolder.

Prototype

public SurfaceHolder getHolder() 

Source Link

Document

Return the SurfaceHolder providing access and control over this SurfaceView's underlying surface.

Usage

From source file:zxing.util.CaptureActivity.java

License:asdf

@Override
protected void onPause() {
    if (handler != null) {
        handler.quitSynchronously();//from  www. j a va2 s  .  c o m
        handler = null;
    }
    inactivityTimer.onPause();
    ambientLightManager.stop();
    cameraManager.closeDriver();
    if (!hasSurface) {
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.removeCallback(this);
    }
    super.onPause();
}

From source file:zxing.util.CaptureActivity.java

License:asdf

@Override
protected void onResume() {
    super.onResume();

    // CameraManager must be initialized here, not in onCreate(). This is
    // necessary because we don't
    // want to open the camera driver and measure the screen size if we're
    // going to show the help on
    // first launch. That led to bugs where the scanning rectangle was the
    // wrong size and partially
    // off screen.
    cameraManager = new CameraManager(getApplication());

    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
    viewfinderView.setCameraManager(cameraManager);

    resultView = findViewById(R.id.result_view);
    statusView = (TextView) findViewById(R.id.status_view);

    handler = null;//from  www  .  ja v a2  s  .  c o  m
    //      lastResult = null;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
        setRequestedOrientation(getCurrentOrientation());
    } else {
        // setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }

    resetStatusView();

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (hasSurface) {
        // The activity was paused but not stopped, so the surface still
        // exists. Therefore
        // surfaceCreated() won't be called, so init the camera here.
        initCamera(surfaceHolder);
    } else {
        // Install the callback and wait for surfaceCreated() to init the
        // camera.
        surfaceHolder.addCallback(this);
    }

    beepManager.updatePrefs();
    ambientLightManager.start(cameraManager);

    inactivityTimer.onResume();

    Intent intent = getIntent();

    decodeFormats = null;
    characterSet = null;

    if (intent != null) {

        //String action = intent.getAction();
        //String dataString = intent.getDataString();
        //characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);

    }
}

From source file:github.nisrulz.qreader.QREader.java

private void startCameraView(Context context, CameraSource cameraSource, SurfaceView surfaceView) {
    if (cameraRunning) {
        throw new IllegalStateException("Camera already started!");
    }/*from  w w  w  . j  a  v a  2s  .com*/
    try {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            Log.e(LOGTAG, "Permission not granted!");
        } else if (!cameraRunning && cameraSource != null && surfaceView != null) {
            cameraSource.start(surfaceView.getHolder());
            cameraRunning = true;
        }
    } catch (IOException ie) {
        Log.e(LOGTAG, ie.getMessage());
        ie.printStackTrace();
    }
}

From source file:org.bobstuff.bobball.ActivityStateEnum.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.main);/*from   w ww .j  a v  a 2  s .c o  m*/

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view);
    surfaceView.setOnTouchListener(this);
    surfaceHolder = surfaceView.getHolder();
    surfaceHolder.setFormat(PixelFormat.RGB_565);
    surfaceHolder.addCallback(this);

    messageView = (TextView) findViewById(R.id.message_label);
    percentageCleared = (TextView) findViewById(R.id.percentageCleared);
    totalPercentageCleared = (TextView) findViewById(R.id.totalPercentageCleared);
    bonusPoints = (TextView) findViewById(R.id.bonusPoints);

    button = (Button) findViewById(R.id.continue_button);
    button.setOnClickListener(this);

    retryButton = (Button) findViewById(R.id.retryButton);
    backToLevelSelectButton = (Button) findViewById(R.id.backToLevelSelectButton);

    statusTopleft = (TextView) findViewById(R.id.status_topleft);
    statusTopright = (TextView) findViewById(R.id.status_topright);
    statusBotleft = (TextView) findViewById(R.id.status_botleft);
    statusBotright = (TextView) findViewById(R.id.status_botright);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawerLayout.setDrawerListener(new DrawerLayout.SimpleDrawerListener() {

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            if (gameManager.isPaused()) {
                gameManager.togglePauseGameLoop();
            }
            changeToPauseScreen();
            onClick(drawerView);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            if (activityState == ActivityStateEnum.GAMERUNNING) {
                if (!gameManager.isPaused()) {
                    gameManager.togglePauseGameLoop();
                }
                showPauseScreen();
            }
        }
    });

    drawerLayout.setFocusableInTouchMode(false); // if set to false, the drawer doesn't react to pressing the back key by default

    statusTopright.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            statusTopright.setTextColor(0xffCCCCFF);
            secretHandshake += 1;
        }
    });

    statusBotright.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (secretHandshake > 4) {
                statusBotright.setTextColor(0xffCCCCFF);
                //request a hidden service
                Intent intent = new Intent("org.torproject.android.REQUEST_HS_PORT");
                intent.setPackage("org.torproject.android");
                intent.putExtra("hs_port", 8477);
                startActivityForResult(intent, 9999);
            }
        }
    });

    Bundle extras = getIntent().getExtras();
    level = extras.getInt("level");
    numPlayers = extras.getInt("numPlayers");

    scores = new Scores(numPlayers);
    scores.loadScores();
}

From source file:com.cleverzone.zhizhi.capture.CaptureActivity.java

@Override
protected void onPause() {
    if (handler != null) {
        handler.quitSynchronously();/*w w  w.  j  a  v  a  2 s.  co  m*/
        handler = null;
    }
    inactivityTimer.onPause();
    //        ambientLightManager.stop();
    beepManager.close();
    cameraManager.closeDriver();
    //historyManager = null; // Keep for onActivityResult
    if (!hasSurface) {
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.removeCallback(this);
    }
    super.onPause();
}

From source file:com.jasompeter.openalpr.CameraActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_camera);

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surface_view);
    mTorchButton = (ImageButton) findViewById(R.id.torch_button);
    mCapturingText = (TextView) findViewById(R.id.capturing_text);
    mTouchToCaptureText = (TextView) findViewById(R.id.touch_to_capture);
    mSurfaceHolder = surfaceView.getHolder();
    mSurfaceHolder.addCallback(this);
    mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    // we should not open camera on UI thread
    mCameraOpenTask = new AsyncTask<Void, Void, Void>() {
        @Override//from   w w w .  j a v a2 s .  c o  m
        protected Void doInBackground(Void[] params) {

            if (!mCameraIsOpen) {
                releaseCameraAndPreview();
                try {

                    int cameraCount = Camera.getNumberOfCameras();
                    Log.d(TAG, "We have " + cameraCount + " cameras.");
                    for (int i = 0; i < cameraCount; i++) {
                        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                        Camera.getCameraInfo(i, cameraInfo);
                        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                            mCameraId = i;
                            break;
                        }
                    }

                    mCamera = Camera.open(mCameraId);
                    mCameraIsOpen = true;
                } catch (Exception e) {
                    mCameraIsOpen = false;
                    Log.d(TAG, "Failed to open camera. Camera is probably in use.");
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void o) {
            onCameraOpened(mCamera != null);
        }
    };

    // orientation manager handles orientation changes
    // because our activity is set to force landscape mode
    // and we may want rotate action buttons or do something with view
    mOrientationManager = new OrientationManager(this, SensorManager.SENSOR_DELAY_NORMAL, this);

    invalidateTorchButton();
    mTorchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            toggleTorch();
        }
    });

    initializeAlpr();

    surfaceView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mEnableRecognition = true;
                mCapturingText.setVisibility(View.VISIBLE);
                mTouchToCaptureText.setVisibility(View.GONE);
                return true; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
                mEnableRecognition = false;
                mCapturingText.setVisibility(View.GONE);
                mTouchToCaptureText.setVisibility(View.VISIBLE);
                return true; // if you want to handle the touch event
            }
            return false;
        }
    });

}

From source file:com.coact.kochzap.CaptureActivity.java

@Override
protected void onResume() {
    super.onResume();

    // historyManager must be initialized here to update the history preference
    historyManager = new HistoryManager(this);
    historyManager.trimHistory();/* www  . ja v  a 2  s .co  m*/

    // CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
    // want to open the camera driver and measure the screen size if we're going to show the help on
    // first launch. That led to bugs where the scanning rectangle was the wrong size and partially
    // off screen.
    cameraManager = new CameraManager(getApplication());

    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
    viewfinderView.setCameraManager(cameraManager);

    resultView = findViewById(R.id.result_view);
    statusView = (TextView) findViewById(R.id.status_view);

    handler = null;
    lastResult = null;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    resetStatusView();

    beepManager.updatePrefs();
    ambientLightManager.start(cameraManager);

    inactivityTimer.onResume();

    Intent intent = getIntent();

    copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true)
            && (intent == null || intent.getBooleanExtra(Intents.Scan.SAVE_HISTORY, true));

    source = IntentSource.NONE;
    sourceUrl = null;
    scanFromWebPageManager = null;
    decodeFormats = null;
    characterSet = null;

    if (intent != null) {

        String action = intent.getAction();
        String dataString = intent.getDataString();

        if (Intents.Scan.ACTION.equals(action)) {
            // Scan the formats the intent requested, and return the result to the calling activity.
            source = IntentSource.NATIVE_APP_INTENT;
            decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
            decodeHints = DecodeHintManager.parseDecodeHints(intent);

            if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) {
                int width = intent.getIntExtra(Intents.Scan.WIDTH, 0);
                int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0);
                if (width > 0 && height > 0) {
                    cameraManager.setManualFramingRect(width, height);
                }
            }

            if (intent.hasExtra(Intents.Scan.CAMERA_ID)) {
                int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1);
                if (cameraId >= 0) {
                    cameraManager.setManualCameraId(cameraId);
                }
            }

            String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
            if (customPromptMessage != null) {
                statusView.setText(customPromptMessage);
            }
        } else if (dataString != null && dataString.contains("http://www.google")
                && dataString.contains("/m/products/scan")) {

            // Scan only products and send the result to mobile Product Search.
            source = IntentSource.PRODUCT_SEARCH_LINK;
            sourceUrl = dataString;
            decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;
        } else if (isZXingURL(dataString)) {

            // Scan formats requested in query string (all formats if none specified).
            // If a return URL is specified, send the results there. Otherwise, handle it ourselves.
            source = IntentSource.ZXING_LINK;
            sourceUrl = dataString;
            Uri inputUri = Uri.parse(dataString);
            scanFromWebPageManager = new ScanFromWebPageManager(inputUri);
            decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri);
            // Allow a sub-set of the hints to be specified by the caller.
            decodeHints = DecodeHintManager.parseDecodeHints(inputUri);

        }
        characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);
    }

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (hasSurface) {
        // The activity was paused but not stopped, so the surface still exists. Therefore
        // surfaceCreated() won't be called, so init the camera here.
        initCamera(surfaceHolder);
    } else {
        // Install the callback and wait for surfaceCreated() to init the camera.
        surfaceHolder.addCallback(this);
    }
}

From source file:com.cleverzone.zhizhi.capture.CaptureActivity.java

@Override
protected void onResume() {
    super.onResume();

    // historyManager must be initialized here to update the history preference
    //        historyManager = new HistoryManager(this);
    //        historyManager.trimHistory();

    // CameraManager must be initialized here, not in onCreate(). This is necessary because we don't
    // want to open the camera driver and measure the screen size if we're going to show the help on
    // first launch. That led to bugs where the scanning rectangle was the wrong size and partially
    // off screen.
    cameraManager = new CameraManager(getApplication());

    viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
    viewfinderView.setCameraManager(cameraManager);

    resultView = findViewById(R.id.result_view);
    statusView = (TextView) findViewById(R.id.status_view);

    handler = null;//  w  w  w . j a v a  2 s.c o  m
    lastResult = null;

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

    //        if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {
    //            setRequestedOrientation(getCurrentOrientation());
    //        } else {
    //            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    //        }
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);

    resetStatusView();

    beepManager.updatePrefs();
    //        ambientLightManager.start(cameraManager);

    inactivityTimer.onResume();

    Intent intent = getIntent();

    //        copyToClipboard = prefs.getBoolean(PreferencesActivity.KEY_COPY_TO_CLIPBOARD, true)
    //                && (intent == null || intent.getBooleanExtra(Intents.Scan.SAVE_HISTORY, true));

    source = IntentSource.NONE;
    sourceUrl = null;
    //        scanFromWebPageManager = null;
    decodeFormats = null;
    characterSet = null;

    if (intent != null) {

        String action = intent.getAction();
        String dataString = intent.getDataString();

        if (Intents.Scan.ACTION.equals(action)) {

            // Scan the formats the intent requested, and return the result to the calling activity.
            source = IntentSource.NATIVE_APP_INTENT;
            decodeFormats = DecodeFormatManager.parseDecodeFormats(intent);
            decodeHints = DecodeHintManager.parseDecodeHints(intent);

            if (intent.hasExtra(Intents.Scan.WIDTH) && intent.hasExtra(Intents.Scan.HEIGHT)) {
                int width = intent.getIntExtra(Intents.Scan.WIDTH, 0);
                int height = intent.getIntExtra(Intents.Scan.HEIGHT, 0);
                if (width > 0 && height > 0) {
                    cameraManager.setManualFramingRect(width, height);
                }
            }

            if (intent.hasExtra(Intents.Scan.CAMERA_ID)) {
                int cameraId = intent.getIntExtra(Intents.Scan.CAMERA_ID, -1);
                if (cameraId >= 0) {
                    cameraManager.setManualCameraId(cameraId);
                }
            }

            String customPromptMessage = intent.getStringExtra(Intents.Scan.PROMPT_MESSAGE);
            if (customPromptMessage != null) {
                statusView.setText(customPromptMessage);
            }

        } else if (dataString != null && dataString.contains("http://www.google")
                && dataString.contains("/m/products/scan")) {

            // Scan only products and send the result to mobile Product Search.
            source = IntentSource.PRODUCT_SEARCH_LINK;
            sourceUrl = dataString;
            decodeFormats = DecodeFormatManager.PRODUCT_FORMATS;

        } else if (isZXingURL(dataString)) {

            // Scan formats requested in query string (all formats if none specified).
            // If a return URL is specified, send the results there. Otherwise, handle it ourselves.
            source = IntentSource.ZXING_LINK;
            sourceUrl = dataString;
            Uri inputUri = Uri.parse(dataString);
            //                scanFromWebPageManager = new ScanFromWebPageManager(inputUri);
            decodeFormats = DecodeFormatManager.parseDecodeFormats(inputUri);
            // Allow a sub-set of the hints to be specified by the caller.
            decodeHints = DecodeHintManager.parseDecodeHints(inputUri);

        }

        characterSet = intent.getStringExtra(Intents.Scan.CHARACTER_SET);

    }

    SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    if (hasSurface) {
        // The activity was paused but not stopped, so the surface still exists. Therefore
        // surfaceCreated() won't be called, so init the camera here.
        initCamera(surfaceHolder);
    } else {
        // Install the callback and wait for surfaceCreated() to init the camera.
        surfaceHolder.addCallback(this);
    }
}

From source file:net.oschina.app.v2.activity.zxing.CaptureActivity.java

@Override
protected void onPause() {
    if (handler != null) {
        handler.quitSynchronously();/*from w  w w .  ja v a  2s.  c o  m*/
        handler = null;
    }
    inactivityTimer.onPause();
    ambientLightManager.stop();
    beepManager.close();
    cameraManager.closeDriver();
    //historyManager = null; // Keep for onActivityResult
    if (!hasSurface) {
        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        SurfaceHolder surfaceHolder = surfaceView.getHolder();
        surfaceHolder.removeCallback(this);
    }
    super.onPause();
}

From source file:net.nanocosmos.bintu.demo.encoder.activities.StreamActivity.java

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    Surface foo = new Surface(surface);
    SurfaceView foo1 = new SurfaceView(this);
    foo = foo1.getHolder().getSurface();
    GLSurfaceView foo2 = new GLSurfaceView(this);
    foo = foo2.getHolder().getSurface();

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        initStreamLib();//from   w  ww.j  a v  a2  s. co m
    } else {
        appPermissions = new CheckAppPermissions(this);
        boolean needPermission = false;
        if (streamVideo) {
            needPermission |= !appPermissions.checkCameraPermissions();
        }
        if (streamAudio) {
            needPermission |= !appPermissions.checkRecordAudioPermission();
        }
        if (recordMp4) {
            needPermission |= !appPermissions.checkWriteExternalStoragePermission();
        }

        if (needPermission) {
            appPermissions.requestMissingPermissions();
        } else {
            initStreamLib();
        }
    }
}