This is my english engineering blog. If you want korean, visit my korean blog. My korean blog is answerofgod.tistory.com
Nov 25, 2014
Nov 19, 2014
surfaceview screenshot (camera screenshot)
Surface view often use for Camera preview.
Because Camera preview need many load.
So, if you capture your phone, camera does'n capture.
May be just black image!!
I tried below image.
But I get just overlay and black display.
So I use camera preview callback!!
First, sufaceview
- public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback{
- SurfaceHolder holder;
- Camera cam=null; //camera
- public SurfacePreview(Context context, AttributeSet attrs) {
- super(context, attrs);
- init(context);
- }
- public SurfacePreview(Context context) {
- super(context);
- init(context);
- }
- public void init(Context context){
- holder=getHolder();
- holder.addCallback(this);
- if(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
- getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {
- cam.setDisplayOrientation(90); //portrait
- cam.startPreview(); //preview start
- }
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- cam=Camera.open(); //open camera object(need camera permission)
- try{
- cam.setPreviewDisplay(holder);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- cam.stopPreview();
- cam.release();
- cam=null;
- }
- }
This is my old code.
I add implement Camera.PreviewCallback!!
- public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback
Add onPreviewFrame method.
- @Override
- 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, 100, out);
- Bitmap bm = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());
- Matrix matrix = new Matrix();
- matrix.postRotate(90);
- Bitmap rotatedBitmap = Bitmap.createBitmap(bm, 0, 0,w, h, matrix, true);
- camWeather.shareBitmap=rotatedBitmap;
- }
onPreviewFrame auto callback!!
Line 4~13: camera preview transform to bitmap.
Line 15~17: bitmap rotate because I captured potrait(see surfaceview line 26)
Line 18 : need for combine to overlay.
Add to surfaceCreated for start preview callback
- cam.setPreviewCallback(this);
Add to surfaceDestroyed for anti method called after release() exception.
- cam.setPreviewCallback(null);
This code is combine to overlay.
- public void capture(){
- Bitmap overlay=Bitmap.createBitmap(shareBitmap.getWidth(),shareBitmap.getHeight(),shareBitmap.getConfig());
- canvas.drawBitmap(shareBitmap, 0,0, null);
- shareLayout.buildDrawingCache();
- Bitmap bm=shareLayout.getDrawingCache();
- canvas.drawBitmap(bm,0,0,null);
- FileOutputStream out;
- String temp="/"+filename;
- try{
- out=new FileOutputStream(Environment.getExternalStorageDirectory().toString()+temp);
- overlay.compress(Bitmap.CompressFormat.JPEG,100, out);
- Toast.makeText(getApplicationContext(),temp, Toast.LENGTH_SHORT).show();
- }catch(Exception e){
- Log.d("screenshot",String.valueOf(e));
- e.printStackTrace();
- }
- }
Line 2,4 shareBitmap: camera bitmap from camera preview callback
Line 2~3 : Setting new bitmap feature then draw canvas.
Line 4 : draw camerapreview to new bitmap.
Line 5~6 : capture overlay layout
Line 7 : draw overlay to new bitmap.
Line 8~ : save to jpg file.
This is run this code.!!
Subscribe to:
Posts (Atom)