You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.9 KiB

package com.example.fragment;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.fragment.app.Fragment;
import com.example.activity.BarActivity;
import com.example.smartglasses.MainActivity;
import com.example.smartglasses.R;
import com.example.otherclass.StereoBMUtil;
import org.opencv.android.OpenCVLoader;
import java.io.IOException;
public class HomeFragment extends Fragment {
private ImageView imageViewResult;
private StereoBMUtil stereoBMUtil;
private TextView textView;
private Bitmap leftBitmap;
private Bitmap rightBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@SuppressLint("ClickableViewAccessibility")
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_home, container, false);
//初始化
if (OpenCVLoader.initDebug()) {
Log.d("TAG", "OpenCVLoader初始化成功");
}
stereoBMUtil = new StereoBMUtil();
ImageView imageViewLeft = view.findViewById(R.id.imageViewLeft);
ImageView imageViewRight = view.findViewById(R.id.imageViewRight);
imageViewResult = view.findViewById(R.id.imageViewResult);
textView = view.findViewById(R.id.result_tv);
Button button = view.findViewById(R.id.button);
Button button1 = view.findViewById(R.id.button1);
// 加载图片
try {
leftBitmap = BitmapFactory.decodeStream(getContext().getAssets().open("Left3.bmp"));
rightBitmap = BitmapFactory.decodeStream(getContext().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);
Log.d("Test", "启动C活动");
BarActivity activity = (BarActivity) getActivity();
// startActivity(intent);
activity.setCurrentFragment(4);
});
// 点击计算后的图片,获取三维坐标数据
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;
});
return view;
}
}