Skip to content
This repository has been archived by the owner on Apr 21, 2024. It is now read-only.

Commit

Permalink
Fix Nexus5x camera rotate 180°
Browse files Browse the repository at this point in the history
  • Loading branch information
XuDaojie committed Sep 2, 2016
1 parent 3d1835a commit fcdfe07
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ compile('com.github.XuDaojie:QRCode-Android:v0.1.0', {
exclude group: 'com.android.support'
})
// 6.0权限控制需要support支持
//compile 'com.android.support:appcompat-v7:$supportVersion'
compile 'com.android.support:appcompat-v7:$supportVersion'
```

## 吃水不忘挖井人
Expand Down
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ dependencies {
})
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
// compile project(':qrcodelib')
compile('com.github.XuDaojie:QRCode-Android:v0.1.0', {
exclude group: 'com.android.support'
})
compile project(':qrcodelib')
// compile('com.github.XuDaojie:QRCode-Android:v0.1.1', {
// exclude group: 'com.android.support'
// })
}
4 changes: 2 additions & 2 deletions qrcodelib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 1
versionName "0.1.0"
versionCode 3
versionName "0.1.1"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
Expand Down Expand Up @@ -82,6 +83,7 @@ public class CaptureActivity extends Activity implements Callback {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.camera);
mActivity = this;
Expand Down Expand Up @@ -195,6 +197,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
handleDecode(result, null);
} else {
new AlertDialog.Builder(CaptureActivity.this)
.setTitle("提示")
.setMessage("此图片无法识别")
.setPositiveButton("确定", null)
.show();
Expand All @@ -210,7 +213,8 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
// 未获得Camera权限
new AlertDialog.Builder(mActivity)
.setMessage("请在App设置中开启摄像头权限后重试")
.setTitle("提示")
.setMessage("请在系统设置中为App开启摄像头权限后重试")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Expand All @@ -222,7 +226,8 @@ public void onClick(DialogInterface dialog, int which) {
} else if (grantResults.length > 0 && requestCode == REQUEST_PERMISSION_PHOTO) {
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
new AlertDialog.Builder(mActivity)
.setMessage("请在App设置中开启文件权限后重试")
.setTitle("提示")
.setMessage("请在系统设置中为App中开启文件权限后重试")
.setPositiveButton("确定", null)
.show();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
import android.view.WindowManager;

import java.util.regex.Pattern;
Expand Down Expand Up @@ -177,7 +178,7 @@ void setDesiredCameraParameters(Camera camera) {
setZoom(parameters);
//setSharpness(parameters);
//modify here
camera.setDisplayOrientation(90);
camera.setDisplayOrientation(getDisplayOrientation());
camera.setParameters(parameters);


Expand Down Expand Up @@ -278,4 +279,36 @@ private void setZoom(Camera.Parameters parameters) {
}
}

private int getDisplayOrientation() {
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

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

int result;
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
result = (info.orientation + degrees) % 360;
result = (360 - result) % 360;
} else {
result = (info.orientation - degrees + 360) % 360;
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public final class CameraManager {
private static final String TAG = CameraManager.class.getSimpleName();
private static final int MIN_FRAME_WIDTH = 240;
private static final int MIN_FRAME_HEIGHT = 240;
private static final int MAX_FRAME_WIDTH = 720;
private static final int MAX_FRAME_HEIGHT = 720;
private static final int MAX_FRAME_WIDTH = 675;
private static final int MAX_FRAME_HEIGHT = 675; // = 5/8 * 1080
private static CameraManager cameraManager;

static {
Expand Down

0 comments on commit fcdfe07

Please sign in to comment.