gpt4 book ai didi

android - 相机的 surfaceview 在 android lollipop os 中不工作

转载 作者:可可西里 更新时间:2023-11-01 18:46:45 26 4
gpt4 key购买 nike

今天我在 android surfaceview for camera customization 中遇到了一个问题。我试过下面的代码。

The Issue occurred when I captured the image, it stops the camera preview and doesn't return to the activity.

下面的代码将在程序中实现。我从 stackoverflow 上的现有引用中获取了这段代码

  1. 支持类。

    public class AndroidCameraSurfaceview extends Activity implements
    SurfaceHolder.Callback {
    TextView testView;

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean preview;

    PictureCallback rawCallback;
    ShutterCallback shutterCallback;
    PictureCallback jpegCallback;
    int displayheight, displaywidth;
    Camera.PreviewCallback previewCallback;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.camerasurfaceview);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
    surfaceHolder = surfaceView.getHolder();

    surfaceHolder.addCallback(this);

    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
    Bundle b = new Bundle();
    b.putByteArray("Image", data);
    Intent intent = new Intent();
    intent.putExtras(b);
    setResult(RESULT_OK, intent);
    finish();
    // refreshCamera();
    }
    };
    }



    public void captureImage(View v) throws IOException {
    // take the picture
    camera.takePicture(null, null, jpegCallback);
    }

    public void refreshCamera() {
    if (surfaceHolder.getSurface() == null) {
    // preview surface does not exist
    return;
    }

    try {
    camera.stopPreview();
    } catch (Exception e) {
    }

    try {
    camera.setDisplayOrientation(90);
    camera.setPreviewDisplay(surfaceHolder);
    camera.startPreview();
    } catch (Exception e) {

    }
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
    int height) {
    if (preview) {
    camera.stopPreview();
    }
    try{
    Camera.Parameters parameters = camera.getParameters();
    List<Size> sizes = parameters.getSupportedPreviewSizes();
    Size optimalSize = getOptimalPreviewSize(sizes, width, height);
    parameters.setPreviewSize(optimalSize.width, optimalSize.height);
    camera.setParameters(parameters);
    try {
    camera.setDisplayOrientation(90);
    camera.setPreviewDisplay(holder);
    camera.startPreview();
    preview = true;
    } catch (IOException e) {
    e.printStackTrace();
    }
    }catch(Exception e){
    System.out.println("Surface Exception---=>"+e);
    }
    }

    public void surfaceCreated(SurfaceHolder holder) {
    camera = Camera.open();
    if (camera != null) {
    Camera.Parameters params = camera.getParameters();
    camera.setDisplayOrientation(90);
    camera.setParameters(params);

    }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    // stop preview and release camera
    camera.stopPreview();
    camera.release();
    camera = null;
    }

    private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
    final double ASPECT_TOLERANCE = 1;
    double targetRatio = (double) w / h;
    if (sizes == null)
    return null;
    Size optimalSize = null;
    double minDiff = Double.MAX_VALUE;
    int targetHeight = h;
    for (Size size : sizes) {
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
    continue;
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }

    if (optimalSize == null) {
    minDiff = Double.MAX_VALUE;
    for (Size size : sizes) {
    if (Math.abs(size.height - targetHeight) < minDiff) {
    optimalSize = size;
    minDiff = Math.abs(size.height - targetHeight);
    }
    }
    }
    return optimalSize;
    }

    }

    2.在Activity中实现

    public void captureImage() {

        Intent intentDriver = new Intent(AddNewDevice_Activity.this,
    AndroidCameraSurfaceview.class);
    startActivityForResult(intentDriver, 0);
    //
    // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //
    // Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    //
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //
    // // start the image capture Intent
    // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

    // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    //
    // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    //
    // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
    //
    // // start the image capture Intent
    // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 0) {

    System.out.println("Result Code: " + resultCode);
    if (resultCode == RESULT_OK && data != null) {

    Bundle bundle = data.getExtras();
    byte[] test = bundle.getByteArray("Image");
    Bitmap bpCamera = BitmapFactory.decodeByteArray(test, 0,
    test.length);
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bpCamera = Bitmap
    .createBitmap(bpCamera, 0, 0, bpCamera.getWidth(),
    bpCamera.getHeight(), matrix, true);

    imageView_camera.setImageBitmap(bpCamera);
    selectedImageStr = encodeTobase64(bpCamera);
    }

    } else {
    finish();
    }

    }

最佳答案

你应该分开 Activity 和表面 View 。

public class AndroidCameraActivity extends Activity {
AndroidCameraSurfaceview surfaceView;
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
surfaceView = (AndroidCameraSurfaceview) findViewById(R.id.surfaceView);
}
}

class AndroidCameraSurfaceview extends SurfaceView implements SurfaceHolder.Callback {
public AndroidCameraSurfaceview(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
...
}

public void surfaceCreated(SurfaceHolder holder) {
...
}

public void surfaceDestroyed(SurfaceHolder holder) {
...
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
...
}
}

关于android - 相机的 surfaceview 在 android lollipop os 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471146/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com