【发布时间】:2015-11-18 05:33:03
【问题描述】:
在我的项目中,我想从实时摄像机源中选择一个对象,然后仅为该选定区域创建一个 JPEG 文件。所以在 onCameraFrame 方法中,我在Imgproc.drawContours(mRgba, contours, 0, CONTOUR_COLOR); 中设置了参数 0 来选择第一个项目,即所选项目。然后我尝试为该项目实现一个绑定矩形。
OnCameraFrame()
公共垫 onCameraFrame(CvCameraViewFrame inputFrame) { mRgba = inputFrame.rgba();
if (mIsColorSelected) {
mDetector.process(mRgba);
List<MatOfPoint> contours = mDetector.getContours();
Log.e(TAG, "Contours count: " + contours.size());
Imgproc.drawContours(mRgba, contours, 0, CONTOUR_COLOR);
Mat colorLabel = mRgba.submat(4, 68, 4, 68);
colorLabel.setTo(mBlobColorRgba);
MatOfPoint2f contour2f = new MatOfPoint2f( contours.get(0).toArray());
//Processing on mMOP2f1 which is in type MatOfPoint2f
double approxDistance = Imgproc.arcLength(contour2f, true)*0.02;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
//Convert back to MatOfPoint
MatOfPoint points = new MatOfPoint( approxCurve.toArray() );
// Get bounding rect of contour
Rect rect = Imgproc.boundingRect(points);
Mat selectedRegion = mRgba.submat(rect);
Bitmap bmp = null;
try {
bmp = Bitmap.createBitmap(selectedRegion.cols(), selectedRegion.rows(), Bitmap.Config.ARGB_8888);
convertBitmapToImage(bmp);
} catch (CvException e) {
Log.d("Exception", e.getMessage());
}
}
return mRgba;
}
然后将mat对象转换为位图并保存为JPEG格式
convertBitmapToImage
private void convertBitmapToImage(Bitmap bmp) {
File root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/Dummy");
if (!root.exists()) {
root.mkdirs();
} else {
System.out.print("Exists");
}
File f = new File(root, "filename.jpeg");
//Convert bitmap to byte array
Bitmap bitmap = bmp;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, bos);
byte[] bitmapdata = bos.toByteArray();
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//write the bytes in file
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
但它会创建一个空白的 JPEG 文件
我做错了什么或需要做什么
【问题讨论】:
-
检查该图像是否保存在外部存储中并且您正在获取该图像的路径
-
这是我刚刚发布的图片路径中保存的图片!!