I'm trying to put a camera preview (SurfaceView) together with a button on the display, but all I get is a blank screen only showing the button. If I'm setting the SurfaceView as the only content (with setContentView(surfaceView) then the preview is displaying fine on the screen. What am I doing wrong?
Thanks in advance
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1">
<SurfaceView
android:id="#+id/camera_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/trigger"
android:id="#+id/trigger_picture_button"
android:layout_gravity="bottom" />
</FrameLayout>
</LinearLayout>
My Activity:
public class CameraActivity extends Activity {
private SurfaceView cameraPreview;
private Button triggerPicture;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
cameraPreview = (SurfaceView) findViewById(R.id.camera_surface);
triggerPicture = (Button) findViewById(R.id.trigger_picture_button);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
cameraPreview = new CameraPreview(this);
// setContentView(cameraPreview);
}
My CameraPreview:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holder;
private Camera camera;
public CameraPreview(Context context) {
super(context);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
camera = openFrontFacingCamera();
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
camera.release();
camera = null;
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
parameters.set("orientation", "portrait");
camera.setParameters(parameters);
camera.startPreview();
}
private Camera openFrontFacingCamera() {
// returns Camera.open(
}
When you declare SurfaceView in the layout you tell Android to use built-in class. What you need to do is make it use your class by replacing SurfaceView in XML with your.package.name.CameraPreview.
One other thing: you should add a constructor to the CameraPreview taking parameters Context and AttributeSet, otherwise layout inflater won't be able to inflate your class. Also you don't need the line cameraPreview = new CameraPreview(this);.
When your onCreate() is the right one, than you overriding the cameraPreview from the xml with a new created one which you never add to any layout (last line). That means its never inflated so there is never a surface created.
Maybe it's a bit late answer ,but it really works for me.
1、set the layout as below:
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
<Button
android:id="#+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
2、In onCreate:
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
3、And in Manifest.xml:
android:screenOrientation="landscape"
, of course , don't forget permissions and features.
Hope it helps.
Related
What I want is when I scroll up the scrollview the gallery should be open and when I scroll down then the surfaceview(camera) should be visible like OLX. I have explained my requirement in the image.hope I explained my question in proper manner.
I have explained my requirement in this image
im also posting my xml codes to let you properly understand.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ali.ansofexperts.Question_Photo">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/camera_preview">
</FrameLayout>
<ScrollView
android:layout_width="fill_parent"
android:id="#+id/scrollView"
android:fillViewport="true"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:minHeight="400dp"
android:layout_height="wrap_content"
android:id="#+id/click"
android:orientation="horizontal">
</LinearLayout>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:columnWidth="90dp"
android:numColumns="auto_fit" android:verticalSpacing="10dp"
android:horizontalSpacing="10dp" android:stretchMode="columnWidth"
android:gravity="center" android:layout_gravity="bottom"
android:layout_marginTop="400dp"></GridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I am posting this Reference for beginner
https://android-arsenal.com/details/1/5987
This Reference have the complete solution about my question,hope this will helpful.
Try this
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
Refer this document you can achieve surfaceview with camer...gallery
party you can try your self..
https://developer.android.com/guide/topics/media/camera.html
https://github.com/googlesamples/android-Camera2Video
I'm new on Android, and i trying to build a camera app.
I build an Camera Preview which extends SurfaceView and implements SurfaceHolder.Callback for previewing the camera on the Camera Activity.
here is the contractor :
public CameraPreview(Context context) {
super(context);
mContext = context;
mStartRequested = false;
mSurfaceAvailable = false;
mCamera = null;
mHolder = getHolder();
mHolder.addCallback(this);
}
and the onCreate(in CameraActivity.java) method which initialize the layout:
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_camera);
mPreview = new CameraPreview(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
and finally the Layout XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>
</LinearLayout>
AndroidMainifest.xml:
<application
...
android:theme="#android:style/Theme.Holo.Light.NoActionBar.Fullscreen" >
As you can see i tried to add a button on the camera preview but i cant see the button when the app is lunching.
can anyone see the mistake??
Thanks you !
The preview is probably on top of the button (in the Z-order). This line:
preview.addView(mPreview);
adds the CameraPreview to the end of the FrameLayout's internal list of child views. A FrameLayout renders its child views in order; as such, the preview is being drawn after the button, or "over" it. Try this instead:
preview.addView(mPreview, 0);
You can also order your CameraPreview by having it inflate from the XML instead, by using a tag with the fully qualified class name, like this:
<com.yourdomain.CameraPreview
.../>
...although you will need to override the View(Context context, AttributeSet attrs); constructor to make that work.
I've created a custom view, seen below. I declare this custom view in other xml files. The issue I'm having is that when I call
MyVideoView.setVisibility(View.GONE)
The video that was previously drawn on the SurfaceView is still visible. Shouldn't setting the visibility of the whole View affect the children? Am I missing something?
public class MyVideoView extends FrameLayout {
private SurfaceView videoSurfaceView;
private SurfaceView subtitleSurfaceView;
public MyVideoView(Context context) {
super(context);
initView(context);
}
private void initView(Context context) {
View.inflate(context, R.layout.widget_my_video_view, this);
videoSurfaceView = (SurfaceView) findViewById(R.id.svPlaybackSurface);
subtitleSurfaceView = (SurfaceView) findViewById(R.id.svSubtitles);
}
}
widget_my_video_view.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/svPlaybackSurface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
<SurfaceView
android:id="#+id/svSubtitles"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginStart="#dimen/paddingRegular"
android:layout_marginEnd="#dimen/paddingRegular"
android:layout_marginBottom="#dimen/paddingLarge"
android:visibility="invisible"/>
</FrameLayout>
In my small demo app, I'm showing the camera preview on a SurfaceView. Works fine. But now I want to use a compound XML for the inner preview, so that I can render for example some text onto the camera picture. The layout on screen is:
class CameraPreview extends ViewGroup implements SurfaceHolder.Callback {
CameraPreview(Context context) {
super(context);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
}
The preview is created in the main activity:
public class CameraActivity extends Activity {
private CameraPreview mPreview;
Camera mCamera;
int numberOfCameras;
int cameraCurrentlyLocked;
int defaultCameraId;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
The simple XML for the main activity:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/camera_preview"
tools:context=".CameraActivity" >
</FrameLayout>
So far so good. But now I added a camera_preview.xml which also includes the SurfaceView that was created in code before.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SurfaceView
android:id="#+id/renderingView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="blabla" />
</FrameLayout>
When I then change to constructor of the class CameraPreview ...
CameraPreview(Context context) {
super(context);
String inflaterService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater) context.getSystemService(inflaterService);
li.inflate(R.layout.camera_preview, this, true);
mSurfaceView = (SurfaceView) findViewById(R.id.renderingView);
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
The problem is, that the resulting screen is just white and I don't see the preview neither the text. But I cannot figure out what I'm doing wrong.
Any help appreciated.
Let me begin with the coding first.
My xml file (relevant part of it) contains this:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffff">
<SurfaceView
android:id="#+id/cameraView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone" />
<LinearLayout
android:id="#+id/layStatus"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
<TextView
android:id="#+id/txtStatus"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#90000000"
android:paddingBottom="5dip"
android:paddingTop="5dip"
android:gravity="center_vertical|center_horizontal"
android:textSize="20dip" />
</LinearLayout>
</FrameLayout>
As you can see I've got a basic set up, a frame layout which contains a surfaceview (which has visibility set to gone) and another layout with a simple TextView.
Here is what I have in my activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
cameraView = (SurfaceView)findViewById(R.id.cameraView);
surfaceHolder = cameraView.getHolder();
surfaceHolder.addCallback(CompassActivity.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void btnCameraOnClick(View target) {
cameraView.setVisibility(View.VISIBLE);
}
Starting the camera preview is done in Surface changed, which fires when visibility of the surface changes.
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
txtStatus.setVisibility(View.Visible);
txtStatus.setText("Starting camera");
camera = Camera.open();
if (camera != null){
try {
camera.setDisplayOrientation(90);
Camera.Parameters parameters = camera.getParameters();
List<Size> sizes = parameters.getSupportedPreviewSizes();
Size size = sizes.get(0);
parameters.setPreviewSize(size.width, size.height);
camera.setParameters(parameters);
camera.setPreviewDisplay(holder);
camera.startPreview();
txtStatis.setVisibility(View.GONE);
} catch (IOException e) {
}
}
}
So what's the problem ? I want to simply display a text while the camera preview starts. To do this, I make the txtStatus visible and set a text to it. When the preview is started, I simply hide it. Well, it doesn't work like this, When I press a button to start camera preview, the UI thread freezes waiting for the preview and my status message is not displayed. How can I fix it ? There is a solution at HERE but I was thinking that maybe there is a simpler one.
Thank you for your time.
Camera start up takes some time - external process shall be started and everything set up. I would speculate, that surface changed callback is executed directly from setVisibility() and blocks UI thread. Solution is pretty simple - just spawn new thread and do camera init there.
In my OCR applicatios I start camera in onResume() - since I always need working preview
You may find my OCR android demos helpful:
http://sourceforge.net/projects/javaocr/
( camera preview, with overlays )