这是我所有的代码(没有太多,所以我想粘贴):

关键是我基本上从教程中复制了所有这些代码。除了我从here获得的decodeYUV方法。

我的手机屏幕分辨率为480x800

我遇到问题的关键方法是带有字节流的onPictureTaken方法。

问题是当我拍照时。位图如下所示:

android - 从Android相机Surface获取RGB值-LMLPHP

public class HuntActivity extends AppCompatActivity implements SurfaceHolder.Callback{

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    Camera.PictureCallback jpegCallback;
    ImageView sbut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hunt);
        sbut = (ImageView) findViewById(R.id.searchbut);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        jpegCallback = new Camera.PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {

                int[] rgbs = new int[480*800*3/2]; //buffer size
                decodeYUV(rgbs, data, 480, 800);
                Bitmap bitmap = Bitmap.createBitmap(rgbs, 480, 800, Bitmap.Config.ARGB_8888);
                sbut.setImageBitmap(bitmap);
                Toast.makeText(getApplicationContext(), "Picture Saved", 2000).show();
                refreshCamera();
            }
        };
    }

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

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

        try {camera = Camera.open();
        } catch (RuntimeException e) {
            System.err.println(e);
            return;
        }
        Camera.Parameters param;
        param = camera.getParameters();
        param.setPreviewSize(800, 480);
        camera.setDisplayOrientation(90);
        camera.setParameters(param);
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {
            System.err.println(e);
            return;
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        refreshCamera();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera = null;
    }


    public void refreshCamera() {
        if (surfaceHolder.getSurface() == null) {return;}
        try {camera.stopPreview();
        } catch (Exception e){}
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {}
    }

...
...

最佳答案

您实现jpegCallback,然后将数据字节视为YUV编码。否。数据字节包含jpg图像。因此,请相应地对待他们。

关于android - 从Android相机Surface获取RGB值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34968847/

10-12 04:12