Skip to content

Commit

Permalink
修复长屏手机预览变形问题;
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhuo810 committed Apr 21, 2022
1 parent 955e034 commit aea8a6e
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ if CameraConfig.RATIO_WIDTH >= CameraConfig.RATIO_HEIGHT {

## Log

- 1.0.8 修复长屏手机预览变形问题;
- 1.0.7 支持横屏模式;修复若干bug;
- 1.0.6 将权限声明放到库里面,免得开发者忘记加.
新增`CameraConfig.NEED_WRITE_STORAGE_PERMISSION`配置项,默认false,用于控制是否申请写存储权限,如果图片存在私有目录,是不需要申请这个权限的。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ protected void onCreate(Bundle savedInstanceState) {

public void takePhoto(View v) {
Intent intent = new Intent(MainActivity.this, CropActivity.class);
// intent.putExtra(CameraConfig.RATIO_WIDTH, 855);
// intent.putExtra(CameraConfig.RATIO_HEIGHT, 541);
intent.putExtra(CameraConfig.RATIO_WIDTH, 855);
intent.putExtra(CameraConfig.RATIO_HEIGHT, 541);
intent.putExtra(CameraConfig.NEED_WRITE_STORAGE_PERMISSION, false);
intent.putExtra(CameraConfig.RATIO_WIDTH, 3);
intent.putExtra(CameraConfig.RATIO_HEIGHT, 4);
// intent.putExtra(CameraConfig.RATIO_WIDTH, 3);
// intent.putExtra(CameraConfig.RATIO_HEIGHT, 4);
intent.putExtra(CameraConfig.PERCENT_LARGE, 0.8f);
intent.putExtra(CameraConfig.MASK_COLOR, 0x2f000000);
intent.putExtra(CameraConfig.TOP_OFFSET, 0);
Expand Down
4 changes: 2 additions & 2 deletions camera-card-crop/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 25
versionCode 6
versionName "1.5"
versionCode 7
versionName "1.6"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,26 @@ public static File saveBitMap(Context context, String filePath, final byte[] dat
int degrees = getExifRotateDegree(filePath);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length,options);
Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length, options);
//图片竖屏
photo = rotateBitmap(photo,degrees);
photo = rotateBitmap(photo, degrees);
int scW = ScreenUtils.getScreenWidth(context);
int scH = ScreenUtils.getScreenHeight(context);
if (scW > scH) {
int c = scW;
scW = scH;
scH = c;
}
float ratio = scW * 1.0f / photo.getWidth();
photo = Bitmap.createScaledBitmap(photo, (int)(photo.getWidth() * ratio), (int)(photo.getHeight() *ratio), true);
if (isNeedCut) {
photo = Bitmap.createBitmap(photo, rectLeft*photo.getWidth()/scW, rectTop* photo.getHeight() / scH , rectWidth*photo.getWidth()/scW, rectHeight* photo.getHeight() / scH);
//减去状态栏的高度
int statusBarHeight = (int) Math.ceil(20 * context.getResources().getDisplayMetrics().density);
scH = scH - statusBarHeight;
//将图片宽度缩放到屏幕宽度一致
float ratio = scW * 1.0f / photo.getWidth();
photo = Bitmap.createScaledBitmap(photo, (int) (photo.getWidth() * ratio), (int) (photo.getHeight() * ratio), true);
if (isNeedCut && rectLeft > 0) {
float widthScale = photo.getWidth() * 1.0f / scW;
float heightScale = photo.getHeight() * 1.0f / scH;
photo = Bitmap.createBitmap(photo, (int) (rectLeft * widthScale), (int) (rectTop * heightScale), (int) (rectWidth * widthScale), (int) (rectHeight * heightScale));
}
if (photo != null) {
compressImageToFile(photo, file);
Expand All @@ -66,10 +72,10 @@ public static File saveBitMap(Context context, String filePath, final byte[] dat

}

private static int getExifRotateDegree(String path){
private static int getExifRotateDegree(String path) {
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL);
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
return getExifRotateDegrees(orientation);
} catch (IOException e) {
e.printStackTrace();
Expand Down Expand Up @@ -115,7 +121,7 @@ private static Bitmap rotateBitmap(Bitmap origin, float degrees) {
}


public static void compressImageToFile(Bitmap bmp,File file) {
public static void compressImageToFile(Bitmap bmp, File file) {
int options = 100;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,22 @@ public CameraView(Context context, Camera camera) {
}
}

private void initCamera(Camera camera) throws Exception{
private void initCamera(Camera camera) throws Exception {
Camera.Parameters parameters = camera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
int width = ScreenUtils.getScreenWidth(getContext());
int height = ScreenUtils.getScreenHeight(getContext());
int min = Math.min(width, height);
int max = Math.max(width, height);
for (Camera.Size size : previewSizes) {
if (size.width / 16 == size.height / 9) {
if (size.width / max == size.height / min) {
parameters.setPreviewSize(size.width, size.height);
break;
}
}
List<Camera.Size> pictureSizes = parameters.getSupportedPictureSizes();
for (Camera.Size size : pictureSizes) {
if (size.width / 16 == size.height / 9) {
if (size.width / max == size.height / min) {
parameters.setPictureSize(size.width, size.height);
break;
}
Expand All @@ -61,7 +65,7 @@ private void initCamera(Camera camera) throws Exception{
parameters.setJpegQuality(100);
camera.setParameters(parameters);
}

/**
* 是否横屏
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public void setRatioAndPercentOfScreen(int w, int h, float percent) {
}
}

// Log.e("XXX", "w=" + w + ",h=" + h + ",percnet=" + percent + ",width=" + width + ",height=" + height);
invalidate();
}

Expand Down Expand Up @@ -149,6 +148,7 @@ public void setTopOffset(int topOffset) {
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.e("width", width+"");
if (width > 0) {
boolean isLand = isLandscape();
drawBgWithoutRect(canvas, isLand);
Expand Down

0 comments on commit aea8a6e

Please sign in to comment.