android view图像,android – 从SurfaceView获取图像到ImageView?

我从使用SurfaceView作为相机preivew获取图像/可绘制或位图时遇到了一些麻烦.

final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1);

ll.addView(cameraSurfaceView); // THIS WORKS

ImageView ivCam = (ImageView) findViewById(R.id.ivCam);

ivCam.setImageBitmap(cameraSurfaceView.getDrawingCache()); // THIS DOESN'T :(

有什么建议?谢谢!

编辑:

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(this);

LinearLayout ll = (LinearLayout)findViewById(R.id.LLS);

ll.addView(cameraSurfaceView); // THIS WORKS

}

///

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback

{

private SurfaceHolder holder;

private Camera camera;

public CameraSurfaceView(Context context)

{

super(context);

//Initiate the Surface Holder properly

this.holder = this.getHolder();

this.holder.addCallback(this);

this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override

public void surfaceCreated(SurfaceHolder holder)

{

try

{

this.camera = Camera.open();

this.camera.setPreviewDisplay(this.holder);

this.camera.setPreviewCallback(new PreviewCallback() {

public void onPreviewFrame(byte[] _data, Camera _camera) {

Camera.Parameters params = _camera.getParameters();

int w = params.getPreviewSize().width;

int h = params.getPreviewSize().height;

int format = params.getPreviewFormat();

YuvImage image = new YuvImage(_data, format, w, h, null);

ByteArrayOutputStream out = new ByteArrayOutputStream();

Rect area = new Rect(0, 0, w, h);

image.compressToJpeg(area, 50, out);

Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());

ImageView ivCam = (ImageView) findViewById(R.id.imageView1);

ivCam.setImageBitmap(bm); /// NULL POINT EX HERE!

}

});

}

catch(IOException ioe)

{

ioe.printStackTrace(System.out);

}

}

@Override

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

{

this.camera.startPreview();

}

@Override

public void surfaceDestroyed(SurfaceHolder holder)

{

this.camera.stopPreview();

this.camera.release();

this.camera = null;

}

public Camera getCamera()

{

return this.camera;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值