parent
8107dbd119
commit
34d7656bc5
@ -0,0 +1,48 @@
|
|||||||
|
package com.yeyupiaoling.teststadiometry;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.AttributeSet;
|
||||||
|
import android.view.TextureView;
|
||||||
|
|
||||||
|
|
||||||
|
public class AutoFitTextureView extends TextureView {
|
||||||
|
|
||||||
|
private int mRatioWidth = 0;
|
||||||
|
private int mRatioHeight = 0;
|
||||||
|
|
||||||
|
public AutoFitTextureView(Context context) {
|
||||||
|
this(context, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AutoFitTextureView(Context context, AttributeSet attrs) {
|
||||||
|
this(context, attrs, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
|
||||||
|
super(context, attrs, defStyle);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
|
||||||
|
* calculated from the parameters. Note that the actual sizes of parameters don't matter, that
|
||||||
|
* is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
|
||||||
|
*
|
||||||
|
* @param width Relative horizontal size
|
||||||
|
* @param height Relative vertical size
|
||||||
|
*/
|
||||||
|
public void setAspectRatio(int width, int height) {
|
||||||
|
if (width < 0 || height < 0) {
|
||||||
|
throw new IllegalArgumentException("Size cannot be negative.");
|
||||||
|
}
|
||||||
|
mRatioWidth = width;
|
||||||
|
mRatioHeight = height;
|
||||||
|
requestLayout();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||||
|
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
|
||||||
|
setMeasuredDimension(mRatioWidth, mRatioHeight);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,94 @@
|
|||||||
|
package com.yeyupiaoling.teststadiometry;
|
||||||
|
|
||||||
|
import android.annotation.SuppressLint;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.graphics.Matrix;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
import org.opencv.android.OpenCVLoader;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
private static final String TAG = MainActivity.class.getName();
|
||||||
|
private ImageView imageViewResult;
|
||||||
|
private StereoBMUtil stereoBMUtil;
|
||||||
|
private TextView textView;
|
||||||
|
private Bitmap leftBitmap;
|
||||||
|
private Bitmap rightBitmap;
|
||||||
|
|
||||||
|
@SuppressLint("ClickableViewAccessibility")
|
||||||
|
@Override
|
||||||
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
|
//初始化
|
||||||
|
if (OpenCVLoader.initDebug()) {
|
||||||
|
Log.d(TAG, "OpenCVLoader初始化成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
stereoBMUtil = new StereoBMUtil();
|
||||||
|
ImageView imageViewLeft = findViewById(R.id.imageViewLeft);
|
||||||
|
ImageView imageViewRight = findViewById(R.id.imageViewRight);
|
||||||
|
imageViewResult = findViewById(R.id.imageViewResult);
|
||||||
|
textView = findViewById(R.id.result_tv);
|
||||||
|
Button button = findViewById(R.id.button);
|
||||||
|
Button button1 = findViewById(R.id.button1);
|
||||||
|
|
||||||
|
// 加载图片
|
||||||
|
try {
|
||||||
|
leftBitmap = BitmapFactory.decodeStream(getAssets().open("Left3.bmp"));
|
||||||
|
rightBitmap = BitmapFactory.decodeStream(getAssets().open("Right3.bmp"));
|
||||||
|
imageViewLeft.setImageBitmap(leftBitmap);
|
||||||
|
imageViewRight.setImageBitmap(rightBitmap);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
// 执行StereoBM算法
|
||||||
|
button.setOnClickListener(v -> {
|
||||||
|
try {
|
||||||
|
Bitmap result = stereoBMUtil.compute(leftBitmap, rightBitmap);
|
||||||
|
imageViewResult.setImageBitmap(result);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// 打开相机activity
|
||||||
|
button1.setOnClickListener(v -> {
|
||||||
|
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
|
||||||
|
startActivity(intent);
|
||||||
|
});
|
||||||
|
|
||||||
|
// 点击计算后的图片,获取三维坐标数据
|
||||||
|
imageViewResult.setOnTouchListener((v, event) -> {
|
||||||
|
// 获取触摸点的坐标 x, y
|
||||||
|
float x = event.getX();
|
||||||
|
float y = event.getY();
|
||||||
|
// 目标点的坐标
|
||||||
|
float[] dst = new float[2];
|
||||||
|
Matrix imageMatrix = imageViewResult.getImageMatrix();
|
||||||
|
Matrix inverseMatrix = new Matrix();
|
||||||
|
imageMatrix.invert(inverseMatrix);
|
||||||
|
inverseMatrix.mapPoints(dst, new float[]{x, y});
|
||||||
|
int dstX = (int) dst[0];
|
||||||
|
int dstY = (int) dst[1];
|
||||||
|
// 获取该点的三维坐标
|
||||||
|
double[] c = stereoBMUtil.getCoordinate(dstX, dstY);
|
||||||
|
String s = String.format("点(%d, %d) 三维坐标:[%.2f, %.2f, %.2f]", dstX, dstY, c[0], c[1], c[2]);
|
||||||
|
Log.d(TAG, s);
|
||||||
|
textView.setText(s);
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,78 @@
|
|||||||
|
package com.yeyupiaoling.teststadiometry;
|
||||||
|
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Matrix;
|
||||||
|
import android.util.Size;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Utils {
|
||||||
|
private static final String TAG = Utils.class.getName();
|
||||||
|
|
||||||
|
// 获取最优的预览图片大小
|
||||||
|
public static Size chooseOptimalSize(final Size[] choices, final int width, final int height) {
|
||||||
|
final Size desiredSize = new Size(width, height);
|
||||||
|
|
||||||
|
// Collect the supported resolutions that are at least as big as the preview Surface
|
||||||
|
boolean exactSizeFound = false;
|
||||||
|
float desiredAspectRatio = width * 1.0f / height; //in landscape perspective
|
||||||
|
float bestAspectRatio = 0;
|
||||||
|
final List<Size> bigEnough = new ArrayList<Size>();
|
||||||
|
for (final Size option : choices) {
|
||||||
|
if (option.equals(desiredSize)) {
|
||||||
|
// Set the size but don't return yet so that remaining sizes will still be logged.
|
||||||
|
exactSizeFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
float aspectRatio = option.getWidth() * 1.0f / option.getHeight();
|
||||||
|
if (aspectRatio > desiredAspectRatio) continue; //smaller than screen
|
||||||
|
//try to find the best aspect ratio which fits in screen
|
||||||
|
if (aspectRatio > bestAspectRatio) {
|
||||||
|
if (option.getHeight() >= height && option.getWidth() >= width) {
|
||||||
|
bigEnough.clear();
|
||||||
|
bigEnough.add(option);
|
||||||
|
bestAspectRatio = aspectRatio;
|
||||||
|
}
|
||||||
|
} else if (aspectRatio == bestAspectRatio) {
|
||||||
|
if (option.getHeight() >= height && option.getWidth() >= width) {
|
||||||
|
bigEnough.add(option);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (exactSizeFound) {
|
||||||
|
return desiredSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bigEnough.size() > 0) {
|
||||||
|
final Size chosenSize = Collections.min(bigEnough, new Comparator<Size>() {
|
||||||
|
@Override
|
||||||
|
public int compare(Size lhs, Size rhs) {
|
||||||
|
return Long.signum(
|
||||||
|
(long) lhs.getWidth() * lhs.getHeight() - (long) rhs.getWidth() * rhs.getHeight());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return chosenSize;
|
||||||
|
} else {
|
||||||
|
return choices[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Bitmap rotateBitmap(Bitmap bitmap, int angle) {
|
||||||
|
Matrix matrix = new Matrix();
|
||||||
|
matrix.postRotate(angle);
|
||||||
|
return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Bitmap> bisectionBitmap(Bitmap bitmap) {
|
||||||
|
List<Bitmap> bitmapList = new ArrayList<>();
|
||||||
|
Bitmap left = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() / 2, bitmap.getHeight(), null, true);
|
||||||
|
bitmapList.add(left);
|
||||||
|
Bitmap right = Bitmap.createBitmap(bitmap, bitmap.getWidth() / 2, 0, bitmap.getWidth() / 2, bitmap.getHeight(), null, true);
|
||||||
|
bitmapList.add(right);
|
||||||
|
return bitmapList;
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
Loading…
Reference in new issue