【发布时间】:2021-07-05 09:46:44
【问题描述】:
我有一个实现 onPictureTaken 的 CameraActivity。在这种方法中,我将图像保存到公共 DCIM 文件夹并将 Intent 发送到另一个 PicturePrevActivity。 这里是发件人代码:
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Matrix rotateMatrix = new Matrix();
rotateMatrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), rotateMatrix, false);
String savefilename = saveBitmapPublic(rotatedBitmap,Environment.DIRECTORY_DCIM,"test.jpg");
if(savefilename != null) {
Intent intent = new Intent(CameraActivity.this,PicturePrevActivity.class);
intent.putExtra("picture", savefilename);
startActivity(intent);
} }
这是在 OnCreate() 中接收 PicturePrevActivty 的代码:
if(getIntent().hasExtra("picture")) {
try {
mBitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("picture"));
mSelectionImageView.setImageBitmap(mMyOpenCVWrapper.getResizedBitmap(mBitmap, MAX_HEIGHT));
List<PointF> points = mMyOpenCVWrapper.findPoints(mBitmap);
mSelectionImageView.setPoints(points);
} catch (Exception e) {
e.printStackTrace();
}
}
为了解释,findPoints 方法在 mBitmap 上检测每个 OpenCV 的边缘,并使用此函数将这些点从 mBitmap 转换为 mSelectionImageView 的视点:
private PointF mapPointToMatrix(PointF point) {
float[] points = new float[] { point.x, point.y };
mSelectionImageView.getImageMatrix().mapPoints(points); //this line is the problem, the matrix differs.
if (points.length > 1) {
return new PointF(points[0], points[1]);
} else {
return null;
}
}
如果我在接收 PicturePrevActivty 中单击按钮后开始此操作,则生成的 Rect 是正确的。 但是当我在接收 PicturePrevActivity 的 OnCreate、OnStart 或 OnResume 过程中启动这些操作时,生成的 Rect 太小了。
这里很有趣,如果我将此代码复制到 onCreateOptionsMenu() 方法,它就像一个魅力:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_menu, menu);
if(getIntent().hasExtra("picture")) {
try {
mBitmap = BitmapFactory.decodeFile(getIntent().getStringExtra("picture"));
mSelectionImageView.setImageBitmap(mMyOpenCVWrapper.getResizedBitmap(mBitmap, MAX_HEIGHT));
List<PointF> points = mMyOpenCVWrapper.findPoints(mBitmap);
mSelectionImageView.setPoints(points);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(PicturePrevActivity.this,"Error reading picture",Toast.LENGTH_LONG).show();
}
}
return true;
}
正如我在代码中所标记的,getImageMatrix() 方法是此构造中的问题。它应该交付 矩阵{[1.856, 0.0, 12.0][0.0, 1.856, 0.0][0.0, 0.0, 1.0]},但它提供 矩阵{[1.0, 0.0, 0.0][0.0, 1.0, 0.0][0.0, 0.0, 1.0]} 如果我在 OnCreate、OnStart 或 OnResume 中调用它。只有在 onCreateOptionsMenu() 中使用时才有效。
有人可以帮我吗?为什么在 onCreateOptionsMenu() 中调用时只返回正确的点 getImageMatrix()?我的错是什么?
编辑 2021-07-16:没有人给我建议吗?
提前非常感谢! 最好的祝福, 麦克
【问题讨论】:
标签: android opencv matrix imageview point