Skip to content

Commit

Permalink
1.0.7 支持横屏模式;修复若干bug;
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouzhuo810 committed Aug 20, 2021
1 parent 82d76a5 commit 955e034
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 51 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
}
dependencies {
implementation 'com.github.zhouzhuo810:CameraCardCropDemo:1.0.6'
implementation 'com.github.zhouzhuo810:CameraCardCropDemo:1.0.7'
}
```

Expand Down Expand Up @@ -129,6 +129,7 @@ if CameraConfig.RATIO_WIDTH >= CameraConfig.RATIO_HEIGHT {

## Log

- 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 @@ -17,24 +17,24 @@
* @author zhouzhuo810
*/
public class MainActivity extends AppCompatActivity {

private ImageView ivPic;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ivPic = (ImageView) findViewById(R.id.iv_pic);
ivPic = findViewById(R.id.iv_pic);
}

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, 4);
intent.putExtra(CameraConfig.RATIO_HEIGHT, 3);
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 All @@ -44,7 +44,7 @@ public void takePhoto(View v) {
intent.putExtra(CameraConfig.IMAGE_PATH, getApplicationContext().getExternalCacheDir().getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
startActivityForResult(intent, 0x01);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand All @@ -57,6 +57,6 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}
}
}


}
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 5
versionName "1.4"
versionCode 6
versionName "1.5"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.File;
Expand Down Expand Up @@ -42,9 +43,15 @@ public static File saveBitMap(Context context, String filePath, final byte[] dat
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
Bitmap photo = BitmapFactory.decodeByteArray(data, 0, data.length,options);
//图片竖屏
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) {
Expand Down Expand Up @@ -91,14 +98,14 @@ private static int getExifRotateDegrees(int exifOrientation) {
return degrees;
}

private static Bitmap rotateBitmap(Bitmap origin, float alpha) {
private static Bitmap rotateBitmap(Bitmap origin, float degrees) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
Matrix matrix = new Matrix();
matrix.setRotate(alpha);
matrix.setRotate(degrees);
Bitmap bitmap = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (bitmap.equals(origin)) {
return bitmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,21 @@ private void initCamera(Camera camera) throws Exception{
Camera.getCameraInfo(CameraUtils.findCameraId(false), info);
int rotation = info.orientation % 360;
parameters.setRotation(rotation);
camera.setDisplayOrientation(90);
camera.setDisplayOrientation(isLandscape() ? 0 : 90);
parameters.setJpegQuality(100);
camera.setParameters(parameters);
}

/**
* 是否横屏
*
* @return 是/否
*/
private boolean isLandscape() {
int screenW = ScreenUtils.getScreenWidth(getContext());
int screenH = ScreenUtils.getScreenHeight(getContext());
return screenW > screenH;
}

public CameraView(Context context, AttributeSet attrs) {
super(context, attrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
Expand Down Expand Up @@ -68,7 +69,7 @@ protected void onCreate(Bundle savedInstanceState) {
}

setContentView(R.layout.ccc_activity_crop);

needStoragePermission = getIntent().getBooleanExtra(CameraConfig.NEED_WRITE_STORAGE_PERMISSION, CameraConfig.DEFAULT_NEED_WRITE_STORAGE_PERMISSION);
ratioWidth = getIntent().getIntExtra(CameraConfig.RATIO_WIDTH, CameraConfig.DEFAULT_RATIO_WIDTH);
ratioHeight = getIntent().getIntExtra(CameraConfig.RATIO_HEIGHT, CameraConfig.DEFAULT_RATIO_HEIGHT);
Expand All @@ -81,25 +82,25 @@ protected void onCreate(Bundle savedInstanceState) {
int rectCornerColor = getIntent().getIntExtra(CameraConfig.RECT_CORNER_COLOR, CameraConfig.DEFAULT_RECT_CORNER_COLOR);


ivTake = (ImageView) findViewById(R.id.iv_take);
ivTake = findViewById(R.id.iv_take);
ivTake.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});

framelayout = (FrameLayout) findViewById(R.id.camera);
framelayout = findViewById(R.id.camera);

ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
ImageView ivBack = findViewById(R.id.iv_back);
ivBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});

final ImageView ivFlash = (ImageView) findViewById(R.id.iv_flash);
final ImageView ivFlash = findViewById(R.id.iv_flash);
ivFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -114,10 +115,10 @@ public void onClick(View v) {
}
});

rectView = (RectView) findViewById(R.id.rect);
rectView = findViewById(R.id.rect);
rectView.setMaskColor(maskColor);
rectView.setCornerColor(rectCornerColor);
rectView.setHintTextAndTextSize((hint == null || hint.length() == 0 ? hint : CameraConfig.DEFAULT_HINT_TEXT), 30);
rectView.setHintTextAndTextSize((hint == null || hint.length() == 0 ? CameraConfig.DEFAULT_HINT_TEXT : hint), 30);
rectView.setTopOffset(topOffset);
rectView.setRatioAndPercentOfScreen(ratioWidth, ratioHeight, percentLarge);
rectView.setOnClickListener(new View.OnClickListener() {
Expand Down Expand Up @@ -199,7 +200,7 @@ protected void onResume() {
resumeCamera();
}
}

} else {
resumeCamera();
}
Expand Down Expand Up @@ -294,5 +295,15 @@ private void releaseCamera() {
}
}

/**
* 是否横屏
*
* @return 是/否
*/
private boolean isLandscape() {
int screenW = ScreenUtils.getScreenWidth(this);
int screenH = ScreenUtils.getScreenHeight(this);
return screenW > screenH;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
Expand Down Expand Up @@ -86,16 +87,41 @@ public void setHintTextAndTextSize(String hint, int textSizeInPixel) {
textPaint.setTextSize(textSize);
}

/**
* 是否横屏
*
* @return 是/否
*/
private boolean isLandscape() {
int screenW = ScreenUtils.getScreenWidth(getContext());
int screenH = ScreenUtils.getScreenHeight(getContext());
return screenW > screenH;
}

public void setRatioAndPercentOfScreen(int w, int h, float percent) {
if (w >= h) {
this.width = (int) (ScreenUtils.getScreenWidth(getContext()) * percent);
this.height = width * h / w;
int screenW = ScreenUtils.getScreenWidth(getContext());
int screenH = ScreenUtils.getScreenHeight(getContext());
if (screenW > screenH) {
//横屏
if (w >= h) {
this.width = (int) ((ScreenUtils.getScreenWidth(getContext()) - dp2px(120f)) * percent);
this.height = width * h / w;
} else {
this.height = (int) ((ScreenUtils.getScreenHeight(getContext())) * percent);
this.width = height * w / h;
}
} else {
this.height = (int) ((ScreenUtils.getScreenHeight(getContext()) - dp2px(100f)) * percent);
this.width = height * w / h;
//竖屏
if (w >= h) {
this.width = (int) (ScreenUtils.getScreenWidth(getContext()) * percent);
this.height = width * h / w;
} else {
this.height = (int) ((ScreenUtils.getScreenHeight(getContext()) - dp2px(100f)) * percent);
this.width = height * w / h;
}
}

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

Expand Down Expand Up @@ -124,16 +150,21 @@ public void setTopOffset(int topOffset) {
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (width > 0) {
drawBgWithoutRect(canvas);
boolean isLand = isLandscape();
drawBgWithoutRect(canvas, isLand);
drawCorner(canvas);
drawText(canvas);
drawText(canvas, isLand);
}
}

private void drawText(Canvas canvas) {
private void drawText(Canvas canvas, boolean isLand) {
if (hintText != null) {
float textWidth = textPaint.measureText(hintText);
canvas.drawText(hintText, getWidth() / 2.0f - textWidth / 2.0f, topRect.bottom - textSize, textPaint);
if (isLand) {
canvas.drawText(hintText, (topRect.width() + topOffset) / 2.0f - textWidth / 2.0f, topRect.bottom - textSize, textPaint);
} else {
canvas.drawText(hintText, getWidth() / 2.0f - textWidth / 2.0f, topRect.bottom - textSize, textPaint);
}
}
}

Expand Down Expand Up @@ -163,31 +194,46 @@ private void drawCorner(Canvas canvas) {
canvas.drawPath(rightBottom, cornerPaint);
}

private void drawBgWithoutRect(Canvas canvas) {
topRect = new RectF(0, 0f + topOffset, getWidth(), (getHeight() - height) / 2.0f + topOffset);
leftRect = new RectF(0, (getHeight() - height) / 2.0f + topOffset, (getWidth() - width) / 2.0f, (getHeight() + height) / 2.0f + topOffset);
rightRect = new RectF((getWidth() + width) / 2.0f, (getHeight() - height) / 2.0f + topOffset, getWidth(), (getHeight() + height) / 2.0f + topOffset);
bottomRect = new RectF(0, (getHeight() + height) / 2.0f + topOffset, getWidth(), getHeight());
private void drawBgWithoutRect(Canvas canvas, boolean isLand) {
if (isLand) {
topRect = new RectF(0, 0f, getWidth() + topOffset, (getHeight() - height) / 2.0f);
leftRect = new RectF(0, (getHeight() - height) / 2.0f, (getWidth() - width) / 2.0f + topOffset, (getHeight() + height) / 2.0f);
rightRect = new RectF((getWidth() + width) / 2.0f + topOffset, (getHeight() - height) / 2.0f, getWidth() + topOffset, (getHeight() + height) / 2.0f);
bottomRect = new RectF(0, (getHeight() + height) / 2.0f, getWidth(), getHeight());
} else {
topRect = new RectF(0, 0f + topOffset, getWidth(), (getHeight() - height) / 2.0f + topOffset);
leftRect = new RectF(0, (getHeight() - height) / 2.0f + topOffset, (getWidth() - width) / 2.0f, (getHeight() + height) / 2.0f + topOffset);
rightRect = new RectF((getWidth() + width) / 2.0f, (getHeight() - height) / 2.0f + topOffset, getWidth(), (getHeight() + height) / 2.0f + topOffset);
bottomRect = new RectF(0, (getHeight() + height) / 2.0f + topOffset, getWidth(), getHeight());
}
canvas.drawRect(topRect, bgPaint);
canvas.drawRect(leftRect, bgPaint);
canvas.drawRect(rightRect, bgPaint);
canvas.drawRect(bottomRect, bgPaint);
}

public int getCropLeft() {
return (int) leftRect.right;
return (int) (isLandscape() ? (getHeight() - bottomRect.top) : leftRect.right);
}

public int getCropTop() {
return (int) topRect.bottom;
return (int) (isLandscape() ? leftRect.width() : topRect.bottom);
}

public int getCropWidth() {
return (int) (rightRect.left - leftRect.right);
if (isLandscape()) {
return (int) (bottomRect.top - topRect.bottom);
} else {
return (int) (rightRect.left - leftRect.right);
}
}

public int getCropHeight() {
return (int) (bottomRect.top - topRect.bottom);
if (isLandscape()) {
return (int) (rightRect.left - leftRect.right);
} else {
return (int) (bottomRect.top - topRect.bottom);
}
}

public void setCornerColor(int rectCornerColor) {
Expand Down
Loading

0 comments on commit 955e034

Please sign in to comment.