Example usage for android.view Surface ROTATION_270

List of usage examples for android.view Surface ROTATION_270

Introduction

In this page you can find the example usage for android.view Surface ROTATION_270.

Prototype

int ROTATION_270

To view the source code for android.view Surface ROTATION_270.

Click Source Link

Document

Rotation constant: 270 degree rotation.

Usage

From source file:com.example.android.mediarecorder.MainActivity.java

public void setCameraDisplayOrientation(int cameraId) {
    Log.i("TESTING", "Set Camera Display Orientation Called");
    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = getWindowManager().getDefaultDisplay().getRotation();

    Log.i("TESTING", "Screen Rotation: " + rotation);
    Log.i("TESTING", "Sensor Orientation: " + sensorOrientation);
    if (sensorOrientation == SENSOR_ORIENTATION_DEFAULT_DEGREES) {
        Log.i("TESTING", "Camera: DEFAULT ROTATION: " + DEFAULT_ORIENTATIONS.get(rotation));

        int degrees = 0;
        switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;//ww  w . j  a va  2s.  c o  m
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
        }

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            orientation = (info.orientation + degrees) % 360;
            orientation = (360 - orientation) % 360; // compensate the mirror
            Log.i("TESTING", "Front Facing Camera Orientation: " + orientation);
        } else { // back-facing
            orientation = (info.orientation - degrees + 360) % 360;
            Log.i("TESTING", "Back Facing Camera Orientation: " + orientation);
        }
        Log.i("TESTING", "Set Camera Display Orientation Finished");
    } else {
        Log.i("TESTING", "Camera: INVERSING ROTATION: " + INVERSE_ORIENTATIONS.get(rotation));
        orientation = INVERSE_ORIENTATIONS.get(rotation);
    }
    mCamera.setDisplayOrientation(orientation);
}

From source file:com.example.android.tflitecamerademo.Camera2BasicFragment.java

/**
 * Sets up member variables related to camera.
 *
 * @param width The width of available size for camera preview
 * @param height The height of available size for camera preview
 *///from  ww w . ja v  a 2s  .  co m
private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG,
                    /*maxImages*/ 2);

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            // noinspection ConstantConditions
            /* Orientation of the camera sensor */
            int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
                    rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
            } else {
                textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
            }

            this.cameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        Log.e(TAG, "Failed to access Camera", e);
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error)).show(getChildFragmentManager(),
                FRAGMENT_DIALOG);
    }
}

From source file:fr.xebia.magritte.classifier.lite.Camera2BasicFragment.java

/**
 * Sets up member variables related to camera.
 *
 * @param width The width of available size for camera preview
 * @param height The height of available size for camera preview
 *//*www . j av a2 s .com*/
private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());
            imageReader = ImageReader.newInstance(largest.getWidth(), largest.getHeight(), ImageFormat.JPEG,
                    /*maxImages*/ 2);

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            // noinspection ConstantConditions
            /* Orientation of the camera sensor */
            int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
                    rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
            } else {
                textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
            }

            this.cameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance(getString(R.string.camera_error)).show(getChildFragmentManager(),
                FRAGMENT_DIALOG);
    }
}

From source file:com.manufacton.cordova.MyCameraManager.java

/**
 * Sets up member variables related to camera.
 *
 * @param width  The width of available size for camera preview
 * @param height The height of available size for camera preview
 *///ww w  . j  a  v  a 2  s . c  o m
private void setUpCameraOutputs(int width, int height) {
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
                    new CompareSizesByArea());

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            //noinspection ConstantConditions
            mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (mSensorOrientation == 90 || mSensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (mSensorOrientation == 0 || mSensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int rotatedPreviewWidth = width;
            int rotatedPreviewHeight = height;
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                rotatedPreviewWidth = height;
                rotatedPreviewHeight = width;
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
            // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
            // garbage capture data.
            int cameraLevel = characteristics.get(CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL);
            if (cameraLevel == CameraCharacteristics.INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY
                    && android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.LOLLIPOP) {
                Log.d(TAG, "LEGACY CAMERA DETECTED & OLD OS DETECTED");
                Size[] sizes = map.getOutputSizes(SurfaceTexture.class);
                for (Size option : sizes) {
                    Log.d(TAG, "available : WIDTH: " + option.getWidth() + " HEIGHT: " + option.getHeight());
                }
                mImageReader = ImageReader.newInstance(640, 480, ImageFormat.JPEG, 2);
            } else {
                Log.d(TAG, "LEGACY CAMERA NOT DETECTED");
                mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedPreviewWidth,
                        rotatedPreviewHeight, maxPreviewWidth, maxPreviewHeight, largest);
                mImageReader = ImageReader.newInstance(mPreviewSize.getWidth(), mPreviewSize.getHeight(),
                        ImageFormat.JPEG, 2);
                Log.d(TAG, "For mPreviewSize: WIDTH: " + mPreviewSize.getWidth() + " HEIGHT: "
                        + mPreviewSize.getHeight());
            }

            mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            // int orientation = getResources().getConfiguration().orientation;
            // if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //     mTextureView.setAspectRatio(
            //             mPreviewSize.getWidth(), mPreviewSize.getHeight());
            // } else {
            //     mTextureView.setAspectRatio(
            //             mPreviewSize.getHeight(), mPreviewSize.getWidth());
            // }

            // Check if the flash is supported.
            Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
            mFlashSupported = available == null ? false : available;

            mCameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

From source file:org.linphone.LinphoneActivity.java

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

    instance = this;
    //        if (isTablet() && getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
    //            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    //        } else if (!isTablet() && getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
    //            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    //        }//from www.  j a  v  a  2  s .c  o  m

    mInterstitialAd = new InterstitialAd(LinphoneActivity.this);
    mInterstitialAd.setAdUnitId("ca-app-pub-4414887992172337/6695240808");

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
        }
    });
    requestNewInterstitial();

    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    }

    if (!LinphoneManager.isInstanciated()) {
        Log.e("No service running: avoid crash by starting the launcher", this.getClass().getName());
        // super.onCreate called earlier
        finish();
        startActivity(getIntent().setClass(this, LinphoneLauncherActivity.class));
        return;
    }

    mPreferences = PreferenceManager.getDefaultSharedPreferences(LinphoneActivity.this);

    //        boolean useFirstLoginActivity = getResources().getBoolean(R.bool.display_account_wizard_at_first_start);
    //        if (LinphonePreferences.instance().isProvisioningLoginViewEnabled()) {
    //            Intent wizard = new Intent();
    //            wizard.setClass(this, RemoteProvisioningLoginActivity.class);
    //            wizard.putExtra("Domain", LinphoneManager.getInstance().wizardLoginViewDomain);
    //            startActivityForResult(wizard, REMOTE_PROVISIONING_LOGIN_ACTIVITY);
    //        }
    //        if (useFirstLoginActivity && LinphonePreferences.instance().isFirstLaunch()) {

    //        if (mPreferences.getBoolean(Config.PREF_IS_LOGIN_SUCCESS, false)) { //  && LinphonePreferences.instance().getAccountCount() < 1
    //            LinphonePreferences.instance().firstLaunchSuccessful();
    //        } else {
    //            if (LinphonePreferences.instance().getAccountCount() > 0) {
    //                LinphonePreferences.instance().deleteAccount(LinphonePreferences.instance().getDefaultAccountIndex());
    //            }
    //            startActivityForResult(new Intent().setClass(this, SetupActivity.class), FIRST_LOGIN_ACTIVITY);
    //        }
    //        }

    boolean useFirstLoginActivity = getResources().getBoolean(R.bool.display_account_wizard_at_first_start);
    if (LinphonePreferences.instance().isProvisioningLoginViewEnabled()) {
        Intent wizard = new Intent();
        //wizard.setClass(this, RemoteProvisioningLoginActivity.class);
        wizard.putExtra("Domain", LinphoneManager.getInstance().wizardLoginViewDomain);
        startActivityForResult(wizard, REMOTE_PROVISIONING_LOGIN_ACTIVITY);
    } else if (useFirstLoginActivity && LinphonePreferences.instance().isFirstLaunch()) {
        if (LinphonePreferences.instance().getAccountCount() > 0) {
            LinphonePreferences.instance().firstLaunchSuccessful();
        } else {
            startActivityForResult(new Intent().setClass(this, SetupActivity.class), FIRST_LOGIN_ACTIVITY);
        }
    }

    //TODO rework
    if (getResources().getBoolean(R.bool.use_linphone_tag)) {
        ContactsManager.getInstance().initializeSyncAccount(getApplicationContext(), getContentResolver());
    } else {
        ContactsManager.getInstance().initializeContactManager(getApplicationContext(), getContentResolver());
    }

    //        if (!LinphonePreferences.instance().isContactsMigrationDone()) {
    //            ContactsManager.getInstance().migrateContacts();
    //            LinphonePreferences.instance().contactsMigrationDone();
    //        }

    setContentView(R.layout.main);
    fragmentsHistory = new ArrayList<FragmentsAvailable>();
    initButtons();

    // following line added by aniruddh
    //mCall = (CallButton) findViewById(R.id.Call);

    currentFragment = nextFragment = FragmentsAvailable.DIALER;
    fragmentsHistory.add(currentFragment);
    if (savedInstanceState == null) {
        if (findViewById(R.id.fragmentContainer) != null) {
            dialerFragment = new DialerFragment();
            dialerFragment.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragmentContainer, dialerFragment, currentFragment.toString()).commit();
            selectMenu(FragmentsAvailable.DIALER);
        }
    }

    mListener = new LinphoneCoreListenerBase() {
        @Override
        public void messageReceived(LinphoneCore lc, LinphoneChatRoom cr, LinphoneChatMessage message) {
            if (!displayChatMessageNotification(message.getFrom().asStringUriOnly())) {
                cr.markAsRead();
            }
            displayMissedChats(getChatStorage().getUnreadMessageCount());
            if (messageListFragment != null && messageListFragment.isVisible()) {
                ((ChatListFragment) messageListFragment).refresh();
            }
        }

        @Override
        public void registrationState(LinphoneCore lc, LinphoneProxyConfig proxy,
                LinphoneCore.RegistrationState state, String smessage) {
            if (state.equals(RegistrationState.RegistrationCleared)) {
                if (lc != null) {
                    LinphoneAuthInfo authInfo = lc.findAuthInfo(proxy.getIdentity(), proxy.getRealm(),
                            proxy.getDomain());
                    if (authInfo != null)
                        lc.removeAuthInfo(authInfo);
                }
            }
        }

        @Override
        public void callState(LinphoneCore lc, LinphoneCall call, LinphoneCall.State state, String message) {
            if (state == State.IncomingReceived) {
                startActivity(new Intent(LinphoneActivity.instance(), IncomingCallActivity.class));
            } else if (state == State.OutgoingInit) {
                if (call.getCurrentParamsCopy().getVideoEnabled()) {
                    startVideoActivity(call);
                } else {
                    startIncallActivity(call);
                }
            } else if (state == State.CallEnd || state == State.Error || state == State.CallReleased) {
                // Convert LinphoneCore message for internalization
                if (message != null && message.equals("Call declined.")) {
                    displayCustomToast(getString(R.string.error_call_declined), Toast.LENGTH_LONG);
                } else if (message != null && message.equals("Not Found")) {
                    displayCustomToast(getString(R.string.error_user_not_found), Toast.LENGTH_LONG);
                } else if (message != null && message.equals("Unsupported media type")) {
                    displayCustomToast(getString(R.string.error_incompatible_media), Toast.LENGTH_LONG);
                } else if (message != null && state == State.Error) {
                    //displayCustomToast(getString(R.string.error_unknown), Toast.LENGTH_LONG);
                    Log.e("", "eroor message: " + message);
                    if (message.contains("Temporarily Unavailable")) {
                        displayCustomToast(getString(R.string.user_not_available), Toast.LENGTH_LONG);
                    } else {
                        displayCustomToast(getString(R.string.dial_valid_number), Toast.LENGTH_LONG);
                    }
                }
                resetClassicMenuLayoutAndGoBackToCallIfStillRunning();
            }

            int missedCalls = LinphoneManager.getLc().getMissedCallsCount();
            displayMissedCalls(missedCalls);
        }
    };

    LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
    if (lc != null) {
        lc.addListener(mListener);
    }

    int missedCalls = LinphoneManager.getLc().getMissedCallsCount();
    displayMissedCalls(missedCalls);

    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    switch (rotation) {
    case Surface.ROTATION_0:
        rotation = 0;
        break;
    case Surface.ROTATION_90:
        rotation = 90;
        break;
    case Surface.ROTATION_180:
        rotation = 180;
        break;
    case Surface.ROTATION_270:
        rotation = 270;
        break;
    }

    LinphoneManager.getLc().setDeviceRotation(rotation);
    mAlwaysChangingPhoneAngle = rotation;

    updateAnimationsState();

    mLinphonePreferences = LinphonePreferences.instance();
    defaultAccountIndex = LinphonePreferences.instance().getDefaultAccountIndex();

}

From source file:com.digitalvotingpass.camera.CameraFragment.java

/**
 * Configures the necessary {@link android.graphics.Matrix} transformation to `mTextureView`.
 * This method should be called after the camera preview size is determined in
 * setUpCameraOutputs and also the size of `mTextureView` is fixed.
 *
 * @param viewWidth  The width of `mTextureView`
 * @param viewHeight The height of `mTextureView`
 *///from  w ww  . jav  a  2 s .  c  om
public void configureTransform(int viewWidth, int viewHeight) {
    Activity activity = getActivity();
    if (null == mTextureView || null == mPreviewSize || null == activity) {
        return;
    }
    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    Matrix matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max((float) viewHeight / mPreviewSize.getHeight(),
                (float) viewWidth / mPreviewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    mTextureView.setTransform(matrix);
    overlay.setRect(CameraFragmentUtil.getScanRect(scanSegment));
}

From source file:tr.com.turkcellteknoloji.turkcellupdater.Utilities.java

@SuppressLint("NewApi")
static int getScreenOrientation(Activity activity) {
    if (Build.VERSION.SDK_INT < 8) {
        switch (activity.getResources().getConfiguration().orientation) {
        case Configuration.ORIENTATION_PORTRAIT:
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        case Configuration.ORIENTATION_LANDSCAPE:
            return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
        default:/*  ww w .java2  s . co m*/
            return ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
        }
    }

    int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;
    int orientation;
    // if the device's natural orientation is portrait:
    if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) && height > width
            || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) && width > height) {
        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        default:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        }
    }
    // if the device's natural orientation is landscape or if the device
    // is square:
    else {
        switch (rotation) {
        case Surface.ROTATION_0:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        case Surface.ROTATION_90:
            orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
            break;
        case Surface.ROTATION_180:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
            break;
        case Surface.ROTATION_270:
            orientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
            break;
        default:
            orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            break;
        }
    }

    return orientation;
}

From source file:org.mklab.mikity.android.MainActivity.java

/**
 * ??????//from w w  w .j  a  v a  2s . c o m
 */
public void controlRotation() {
    final Configuration configuration = getResources().getConfiguration();
    final int rotation = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getRotation();
    boolean isReverse = false;

    if (this.settingsFragment.rotationLockButton.isChecked()) {
        switch (rotation) {
        case Surface.ROTATION_180:
        case Surface.ROTATION_270:
            isReverse = true;
            break;
        default:
            isReverse = false;
            break;
        }

        if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            if (isReverse) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        } else {
            if (isReverse) {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
            } else {
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
        MainActivity.this.canvasFragment.objectRenderer.updateDisplay();
    } else {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);
    }
}

From source file:com.microblink.barcode.customcamera.camera2.Camera2Fragment.java

/**
 * Sets up member variables related to camera.
 *
 * @param width  The width of available size for camera preview
 * @param height The height of available size for camera preview
 *//*from   w  w w.  ja v  a2 s.c om*/
private void setUpCameraOutputs(int width, int height) {
    Activity activity = getActivity();
    CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
    try {
        for (String cameraId : manager.getCameraIdList()) {
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);

            // We don't use a front facing camera in this sample.
            Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
            if (facing != null && facing == CameraCharacteristics.LENS_FACING_FRONT) {
                continue;
            }

            StreamConfigurationMap map = characteristics
                    .get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            if (map == null) {
                continue;
            }

            // For still image captures, we use the largest available size.
            Size largest = Collections.max(Arrays.asList(map.getOutputSizes(ImageFormat.YUV_420_888)),
                    new CompareSizesByArea());

            // Find out if we need to swap dimension to get the preview size relative to sensor
            // coordinate.
            int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
            boolean swappedDimensions = false;
            switch (displayRotation) {
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
                if (sensorOrientation == 90 || sensorOrientation == 270) {
                    swappedDimensions = true;
                }
                break;
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                if (sensorOrientation == 0 || sensorOrientation == 180) {
                    swappedDimensions = true;
                }
                break;
            default:
                Log.e(TAG, "Display rotation is invalid: " + displayRotation);
            }

            Point displaySize = new Point();
            activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
            int maxPreviewWidth = displaySize.x;
            int maxPreviewHeight = displaySize.y;

            if (swappedDimensions) {
                maxPreviewWidth = displaySize.y;
                maxPreviewHeight = displaySize.x;
            }

            if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
                maxPreviewWidth = MAX_PREVIEW_WIDTH;
            }

            if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
                maxPreviewHeight = MAX_PREVIEW_HEIGHT;
            }

            // Danger, W.R.! Attempting to use too large a preview size could  exceed the camera
            // bus' bandwidth limitation, resulting in gorgeous previews but the storage of
            // garbage capture data.
            mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), maxPreviewWidth,
                    maxPreviewHeight, largest);

            Log.i(TAG, "Preview size is " + mPreviewSize.toString());

            mImageReader = ImageReader.newInstance(mPreviewSize.getWidth(), mPreviewSize.getHeight(),
                    ImageFormat.YUV_420_888, /*maxImages*/1);
            mImageReader.setOnImageAvailableListener(mOnImageAvailableListener, mBackgroundHandler);

            // We fit the aspect ratio of TextureView to the size of preview we picked.
            int orientation = getResources().getConfiguration().orientation;
            if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
                mTextureView.setAspectRatio(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            } else {
                mTextureView.setAspectRatio(mPreviewSize.getHeight(), mPreviewSize.getWidth());
            }

            mCameraId = cameraId;
            return;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        // Currently an NPE is thrown when the Camera2API is used but not supported on the
        // device this code runs.
        ErrorDialog.newInstance("Camera error").show(getChildFragmentManager(), FRAGMENT_DIALOG);
    }
}

From source file:com.digitalvotingpass.camera.CameraFragment.java

/**
 * Extract a bitmap from the textureview of this fragment.
 * @return//from   w ww  . j  a  va 2s  .c  o m
 */
public Bitmap extractBitmap() {
    try {
        Bitmap bitmap = mTextureView.getBitmap();
        int rotate = Surface.ROTATION_0;
        switch (getActivity().getWindowManager().getDefaultDisplay().getRotation()) {
        case Surface.ROTATION_0:
            rotate = 0;
            break;
        case Surface.ROTATION_90:
            rotate = 270;
            break;
        case Surface.ROTATION_180:
            rotate = 180;
            break;
        case Surface.ROTATION_270:
            rotate = 90;
            break;
        }
        if (rotate != Surface.ROTATION_0) {
            bitmap = CameraFragmentUtil.rotateBitmap(bitmap, rotate);
        }
        Bitmap croppedBitmap = CameraFragmentUtil.cropBitmap(bitmap, scanSegment);

        return CameraFragmentUtil.getResizedBitmap(croppedBitmap, croppedBitmap.getWidth(),
                croppedBitmap.getHeight());
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}