the onMeasure method is overridden in TextureView in the official demo, as follows:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width < height * mRatioWidth / mRatioHeight) {
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
this code maximizes the display of TextureView without exceeding the width and height of the phone screen. The
solution is to make the TextureView always reach the maximum boundary, and the excess part is not previewed (it can actually be photographed). The code is as follows:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
if (0 == mRatioWidth || 0 == mRatioHeight) {
setMeasuredDimension(width, height);
} else {
if (width > height * mRatioWidth / mRatioHeight) { //""""
setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
} else {
setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
}
}
}
is actually replacing "<" in the judgment condition with ">". God knows how many programmers this small piece of code has harmed. No, no, no.
@ mo_chan Why didn't I see the big guy's answer earlier?
my requirements are different: while previewing the full screen, send the preview image to the object detection algorithm, get the detection result (containing the matrix information of the identified object, which can be framed on the incoming image) and draw a detection box on the preview image in real time.
when using a method such as @ mo_chan , you need to zoom the image to ensure that the frame can be accurately painted on the object, but zooming will cause the preview to stretch, so you finally use a compromise: make a "fit" between the resolution provided by the camera and the screen resolution, and make the preview size as close to the full screen as possible without stretching.
here wrote a blog: Portal
solved my problem, really strong. I can't figure it out by myself. How did you analyze it?