上面的代码没有cameraHelper.setPreviewCallback
时间: 2024-02-24 16:55:11 浏览: 115
非常抱歉,我的回答有误。你是对的,确实需要使用`cameraHelper.setPreviewCallback`方法来设置相机预览回调。以下是修改后的代码:
```
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import androidx.appcompat.app.AppCompatActivity;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.github.chrisbanes.photoview.PhotoView;
import com.herohan.uvcapp.CameraHelper;
import com.herohan.uvcapp.ICameraHelper;
import java.io.IOException;
public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {
private static final String TAG = "MainActivity";
private ICameraHelper cameraHelper;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private PhotoView photoView;
private byte[] lastFrameData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化相机助手
cameraHelper = new CameraHelper(this);
// 获取SurfaceView和PhotoView对象
surfaceView = findViewById(R.id.surface_view);
photoView = findViewById(R.id.photo_view);
// 设置PhotoView的缩放类型
photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
// 设置PhotoView的缩放最小值和最大值
photoView.setMinimumScale(0.5f);
photoView.setMaximumScale(10f);
// 获取SurfaceHolder对象并添加回调
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
@Override
protected void onResume() {
super.onResume();
// 打开相机
cameraHelper.openCamera();
}
@Override
protected void onPause() {
super.onPause();
// 关闭相机
cameraHelper.closeCamera();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 设置相机预览的SurfaceHolder
cameraHelper.setPreviewDisplay(holder);
// 开始预览
cameraHelper.startPreview();
// 设置相机预览回调
cameraHelper.setPreviewCallback(new ICameraHelper.PreviewCallback() {
@Override
public void onPreviewFrame(byte[] data, int width, int height) {
Log.d(TAG, "onPreviewFrame: " + data.length);
// 如果上一帧数据还没有处理完,则直接返回
if (lastFrameData != null) {
return;
}
// 将当前帧数据保存到lastFrameData中
lastFrameData = data.clone();
// 使用Glide库加载Bitmap对象到PhotoView中
RequestOptions options = new RequestOptions()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error);
Glide.with(MainActivity.this)
.load(getBitmapFromFrame(data, width, height))
.apply(options)
.into(photoView);
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// 停止预览
cameraHelper.stopPreview();
}
// 按钮点击事件,切换相机前后摄像头
public void switchCamera(View view) {
cameraHelper.switchCamera();
}
// 将相机预览数据转换为Bitmap对象
private Bitmap getBitmapFromFrame(byte[] data, int width, int height) {
Bitmap bitmap = null;
try {
YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream);
byte[] jpegData = outputStream.toByteArray();
bitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
}
```
在`MainActivity`中实现了`SurfaceHolder.Callback`接口,将`SurfaceView`的`SurfaceHolder`对象传递给相机助手对象,然后在`surfaceCreated`回调方法中,设置相机预览回调,将相机预览数据转换为`Bitmap`对象并加载到`PhotoView`中。
同时将相机预览回调中的数据保存到`lastFrameData`中,如果上一帧数据还没有处理完,则直接返回,保证每次只处理一帧数据。
阅读全文