Skip to content

Commit

Permalink
camera2放开更多预览比例,可动态调整预览比例
Browse files Browse the repository at this point in the history
  • Loading branch information
ailiwean committed Oct 23, 2020
1 parent 61c4575 commit 8cb1239
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 24 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/android/NBZxing/CusScanView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ class CusScanView @JvmOverloads constructor(context: Context, attributeSet: Attr
parseFile(string)
}

override fun provideAspectRatio(): AspectRatio {
return AspectRatio.of(1, 1)
}

override fun resultBackFile(content: String) {
if (content.isEmpty())
Toast.makeText(context, "未扫描到内容", Toast.LENGTH_SHORT).show()
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/android/NBZxing/ScanActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import android.view.Window
import android.view.WindowManager
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.google.android.cameraview.AspectRatio
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import kotlinx.android.synthetic.main.activity_main.*

class ScanActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -103,5 +105,14 @@ class ScanActivity : AppCompatActivity() {
}
}

var a = 0

fun change(view: View) {
if (a % 2 == 0)
zxingview.setAspectRatio(AspectRatio.of(1, 1))
else zxingview.setAspectRatio(AspectRatio.of(16, 9))
a++;
}


}
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@

<include layout="@layout/tool_title" />

<Button
android:layout_width="50dp"
android:layout_height="50dp"
android:visibility="visible"
android:layout_gravity="center_vertical"
android:onClick="change" />

</FrameLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ void setUpPreview() {

@Override
boolean isCameraOpened() {
return mCamera != null;
synchronized (Camera1.class) {
return mCamera != null;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.Surface;

Expand All @@ -43,9 +42,7 @@
import com.ailiwean.core.helper.ScanHelper;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

Expand Down Expand Up @@ -256,7 +253,9 @@ void stop() {

@Override
boolean isCameraOpened() {
return mCamera != null;
synchronized (Camera2.class) {
return mCamera != null;
}
}

@Override
Expand Down Expand Up @@ -480,9 +479,10 @@ private boolean collectCameraInfo() {
for (android.util.Size size : map.getOutputSizes(mPreview.getOutputClass())) {
int width = size.getWidth();
int height = size.getHeight();
if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) {
mPreviewSizes.add(new Size(width, height));
}
// if (width <= MAX_PREVIEW_WIDTH && height <= MAX_PREVIEW_HEIGHT) {
// mPreviewSizes.add(new Size(width, height));
// }
mPreviewSizes.add(new Size(width, height));
}

mPictureSizes.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public void cusAction(byte[] data, int dataWidth, int dataHeight) {
dataWidth -= dataHeight;

Rect rect = getScanByteRect(dataWidth, dataHeight);
if (dataWidth == dataHeight)
dataWidth--;
PlanarYUVLuminanceSource source = buildLuminanceSource(data, dataWidth, dataHeight, rect);
result = toLaunchParse(new HybridBinarizerFine(source));
if (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public static Rect getScanByteRect(int dataWidth, int dataHeight) {
RectF cropRect = Config.scanRect.getRect();

//默认采集的数据
if (dataWidth > dataHeight) {
if (dataWidth >= dataHeight) {
if (Config.scanRect.getScanR() == null) {
Config.scanRect.setScanR(new Rect());
Config.scanRect.getScanR().left = (int) (cropRect.top * dataWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ public PlanarYUVLuminanceSource(byte[] yuvData,
super(width, height);

if (left + width > dataWidth || top + height > dataHeight) {
throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
this.yuvData = new byte[0];
this.matrix = this.yuvData;
this.dataHeight = 0;
this.dataWidth = 0;
this.left = 0;
this.top = 0;
return;
}

this.yuvData = yuvData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.os.Handler
import android.os.HandlerThread
import android.util.AttributeSet
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
Expand Down Expand Up @@ -173,6 +174,19 @@ abstract class BaseCameraView @JvmOverloads constructor(context: Context, attrib
isShoudCreateOpen = false
}

override fun setAspectRatio(ratio: AspectRatio) {
super.setAspectRatio(ratio)
//相机运行过程中切换比例
if (isCameraOpened) {
closeCameraBefore()
openCameraBefore()
cameraHandler.post {
stop()
start()
}
}
}

private val cameraHandler by lazy {
val handlerThread = HandlerThread(System.currentTimeMillis().toString())
handlerThread.start()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,12 @@
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

Expand Down Expand Up @@ -128,12 +125,12 @@ public CameraView(Context context, AttributeSet attrs, int defStyleAttr) {
setBackgroundColor(Color.BLACK);
// Internal setup
mCallbacks = new CallbackBridge();
if (Build.VERSION.SDK_INT < 21) {
mImpl = new Camera1(mCallbacks);
} else {
mImpl = new Camera2(mCallbacks, context);
}
// mImpl = new Camera1(mCallbacks);
// if (Build.VERSION.SDK_INT < 21) {
// mImpl = new Camera1(mCallbacks);
// } else {
// mImpl = new Camera2(mCallbacks, context);
// }
mImpl = new Camera1(mCallbacks);
// Attributes
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CameraView, defStyleAttr,
R.style.Widget_CameraView);
Expand Down Expand Up @@ -188,13 +185,14 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = getMeasuredWidth();
int height = getMeasuredHeight();
AspectRatio ratio = getAspectRatio();
if (ratio == null)
return;
if (getContext().getResources().getConfiguration().orientation
== Configuration.ORIENTATION_PORTRAIT) {
Config.displayOrientation = 0;
ratio = ratio.inverse();
}
assert ratio != null;
//当显示的宽高比,与相机输出的宽高比不同时
//当实际略宽时, 调整高度保证与输出比例相同
if (height < width * ratio.getY() / ratio.getX()) {
mImpl.getView().measure(
Expand Down Expand Up @@ -242,7 +240,6 @@ protected void defineScanParseRect(View view) {
if (ratio == null)
return;

//当实际略宽时, 调整高度保证与输出比例相同
if (oriHeight < oriWidth * ratio.getY() / ratio.getX()) {
int measureHeight = (int) (oriWidth * ratio.getY() / (float) ratio.getX());
float expectRatio = (measureHeight - oriHeight) / 2f / measureHeight;
Expand All @@ -254,9 +251,7 @@ protected void defineScanParseRect(View view) {
Config.scanRect.setRect(r);
Config.scanRect.setExtraX(0);
Config.scanRect.setExtraY(measureHeight - oriHeight);
}
//当实际略高时,调整宽度保证与输出比例相同
else {
} else {
int measureWidht = (int) (oriHeight * ratio.getX() / (float) ratio.getY());
float expectRatio = (measureWidht - oriWidth) / 2f / measureWidht;
float[] edgeRatio = findEdgeRatio(view, measureWidht, oriHeight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
android:id="@+id/scanRectView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginBottom="50dp"
android:layout_marginEnd="30dp"
android:layout_gravity="center" />


Expand Down

0 comments on commit 8cb1239

Please sign in to comment.