I want to add SurfaceView which displaying camera preview to my layout. It should be semi-transparent (alpha 0.5). I add View by addView() method.
Ho to do that. I tried animation but it seems to not works with camera.
My Surfave view looks like that:
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e) {
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}
My minAPI = 8.
Related
I have seen many examples on how to live stream video from android camera to rtmp server using surfaceview. One is here : https://github.com/begeekmyfriend/yasea
But is it possible to stream the video from camera to rtmp using a textureview? If it is, how can we?
Textureview mTextureView;
// inside oncreate
mTextureView = (TextureView) findViewById(R.id.texture_view);
mTextureView.setSurfaceTextureListener(AircraftControlActivity.this);
// Outside OnCreate
#Override
public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
}
#Override
public void onSurfaceTextureUpdated(final SurfaceTexture surface) {
}
What to do next?
Take a look at Texture View
public class MainActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener
{
Camera camera;
TextureView textureView;
ImageButton button ; //ignore this one
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textureView = (TextureView) findViewById(R.id.textureView);
button = (ImageButton)findViewById(R.id.imageButton);
textureView.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height)
{
camera = Camera.open();
try
{
camera.setPreviewTexture(surface);
camera.startPreview();
}
catch (IOException ioe)
{
// Something bad happened
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height)
{
// Ignored, Camera does all the work for us
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface)
{
camera.stopPreview();
camera.release();
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface)
{
// Invoked every time there's a new Camera preview frame
}
}
I'm working on a video into a SurfaceView.
My goal is to get recurrent bitmaps of the running video.
Here is my Custom implementation:
private static final String TAG = "XXX";
private Activity activity;
private SurfaceHolder mSurface;
private MediaPlayer mMediaPlayer;
private SurfaceHolder mActiveSurface;
public ImageView imageView;
boolean locked, locked1;
private boolean isCreated;
public AlphaSurfaceView(Context context,Activity activity) {
super(context);
getHolder().addCallback(this);
setWillNotDraw(false);
this.activity = activity;
}
public AlphaSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlphaSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
#Override
public void onDraw(Canvas canvas) {
//super.onDraw(canvas);
if(isCreated){
if(getHolder() != null && getHolder().getSurface().isValid()){
Canvas c = null;
try {
if(!locked){
try {
c = getHolder().lockCanvas();
locked = true;
}catch (IllegalArgumentException e){
e.printStackTrace();
locked = false;
}
BitmapDrawable bdrawable = new BitmapDrawable();
bdrawable.draw(c);
}
}catch (Exception e){
e.printStackTrace();
locked = false;
}finally {
if(c != null && locked){
getHolder().unlockCanvasAndPost(c);
locked = false;
}
}
}
}else{
super.onDraw(canvas);
}
invalidate();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
mSurface = holder;
mMediaPlayer = MediaPlayer.create(getContext(), Uri.parse("XX"), mSurface);
mActiveSurface = mSurface;
try {
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
mMediaPlayer.setLooping(true);
}
});
} catch (Exception e) {
e.printStackTrace();
}
this.isCreated = true;
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.d(TAG, "surfacechanged");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mMediaPlayer.stop();
}
#Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mMediaPlayer.stop();
}
The problem is an IllegalArgumentException occured by the lockCanvas() method.
I tried many possibilities as:
Adding some boolean (locked & creation of surfaceview)
Test if surface is valid
even add exported="true" in targeted activity in Manifest's xml
By the way the first idea was to use getDrawingCache() but even i added setCacheEnabled(true), the return was null.
So how to resolve this Exception or using another way to get each frame?
Thanks!
You can't draw on a Surface and send a video to it. A Surface is the producer end of a producer-consumer pair, and you can only have one producer at a time.
The easiest way to have a Canvas overlap a SurfaceView Surface is to draw on the View part of the SurfaceView. Send the video to the Surface, and use onDraw() (like a custom view) to draw on the View.
Bear in mind that the Surface is a separate layer that sits behind the View UI layer, so you will need to draw on the View with transparency to see the Surface contents.
Another approach is to use multiple overlapping SurfaceViews, but this is less efficient and more limited. An example with three overlapping Surfaces can be found in Grafika's multi-surface test Activity.
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
Log.e("video","surfaceChanged");
}
#Override
public void surfaceCreated(SurfaceHolder arg0) {
Log.e("video","surfaceCreated");
mediaPlayer.setDisplay(surfaceHolder);
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
Log.e("video","surfaceDestroyed");
if (mediaPlayer != null) mediaPlayer.release();
}
});
That should help.
Let the MediaPlayer do the drawing thing.
I am making an app for reading small text but i am not able to get sharp focus . i am using SCENE_MODE_BARCODE for scene mode for small text but it seems to be not working i tried autofocus also but no result either.
sorry for bad english.
my preview class code is
public class MyView extends SurfaceView implements SurfaceHolder.Callback {
Camera surfacecmr;
SurfaceHolder mholder;
#SuppressWarnings("deprecation")
public MyView(Context context, Camera cmr) {
super(context);
surfacecmr = cmr;
mholder = getHolder();
mholder.addCallback(this);
mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
// surfacecmr.setDisplayOrientation(90);
surfacecmr.setPreviewDisplay(holder);
surfacecmr.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (mholder.getSurface() == null)
return;
surfacecmr.stopPreview();
// get Camera parameters
Camera.Parameters params = surfacecmr.getParameters();
params.setSceneMode(Camera.Parameters.SCENE_MODE_BARCODE);
surfacecmr.setParameters(params);
try {
surfacecmr.setPreviewDisplay(mholder);
} catch (IOException e) {
e.printStackTrace();
}
surfacecmr.startPreview();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfacecmr.release();
}
}
I was trying to write a simple camera app, but I ran into quite a frustrating problem with my picture preview. It shows horizontal flicker lines running up the screen. My screen has a width of 800 (Galaxy SII plus), so there are black spaces on the edges (the activity is set to horizontal orientation). Now these black margins begin to transition to a white-grey at the bottom ends when the flicker occurs, so I suspect there might be something drawn over them.
The really annoying part is that this does not seem to depend on the actual code, since it sometimes works without any flickering.
public class Preview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera camera;
private Point size;
Preview(Context context, Camera camera, Point size) {
super(context);
this.camera = camera;
this.size = size;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
setWillNotDraw(false);
Parameters parameters = camera.getParameters();
parameters.setPreviewSize(720, 480);
parameters.setPictureSize(720, 480);
camera.setParameters(parameters);
Camera.Size previewSize = camera.getParameters().getPreviewSize();
setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
previewSize.height, Gravity.CENTER));
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
if (mHolder.getSurface() == null) {
return;
}
// stop preview before making changes
try {
camera.stopPreview();
} catch (Exception e) {
}
Camera.Size previewSize = camera.getParameters().getPreviewSize();
setLayoutParams(new FrameLayout.LayoutParams(previewSize.width,
previewSize.height, Gravity.CENTER));
try {
camera.setPreviewDisplay(mHolder);
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera.setPreviewDisplay(holder);
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
#Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
}
public void setCamera(Camera camera) {
if (this.camera == camera) {
return;
}
stopPreviewAndFreeCamera();
this.camera = camera;
if (this.camera != null) {
requestLayout();
try {
this.camera.setPreviewDisplay(mHolder);
} catch (IOException e) {
e.printStackTrace();
}
this.camera.startPreview();
}
}
public void stopPreviewAndFreeCamera() {
if (this.camera != null) {
this.camera.stopPreview();
this.camera.release();
this.camera = null;
}
}
public void takePicture() {
camera.autoFocus(null);
camera.takePicture(null, null, new PictureHandler(this.getContext()));
camera.startPreview();
}
}
public class MainActivity extends Activity {
private Preview mPreview;
.
.
.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
this.mPreview.stopPreviewAndFreeCamera();
finish();
}
return super.onKeyDown(keyCode, event);
}
}
Hi
I am trying to use the camera to capture an image in one of my application. What is special is that I need a square preview area (and picture in the end). I tried defining the size of both picture and preview to 1:1 pixel ratios, but nothing seams to work. No matter what I does the picture looks "squashed" on a square.
Anyone who has any idea about how to resolve this?
code:
public class AddFromCameraActivity extends Activity implements SurfaceHolder.Callback {
private Camera mCamera;
private Parameters mParameters;
private SurfaceView mCameraPreview;
private SurfaceHolder mSurfaceHolder;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addimagefromcameramain);
initialise();
//Testing area
mCamera = Camera.open();
mParameters = mCamera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mParameters.setJpegQuality(50);
mParameters.setJpegThumbnailQuality(50);
mParameters.setPictureSize(1024, 1024);
//mParameters.setPreviewFormat(ImageFormat.JPEG);
mParameters.setJpegThumbnailSize(256, 256);
mParameters.setPreviewSize(500, 500);
mCamera.setParameters(mParameters);
}
private void initialise()
{
mCameraPreview = (SurfaceView)findViewById(R.id.cameraSurfaceView);
mSurfaceHolder = mCameraPreview.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceHolder.setFixedSize(500, 500);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.startPreview();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
#Override
public void onPause()
{
mCamera.release();
}
}
thanks
You can take a look at the CameraPreview sample code from the Android SDK. The getOptimalPreviewSize method shows how to deal with different camera sizes and the onLayout method shows how to layout the preview surface in the activity.