Overlay images onto Camera preview SurfaceView and how to save them?
I think you are asking how to merge the two images into one.
The first thing you should do is use an AsynchHandler to save your image so you don't lock up the UI, that's not part of your issue but it will be.
As to your general question it was asked and answered here in general you will need to merge the two image parts together and handle scaling of your overlay as a whole, or the component image parts.
How to draw an overlay on a SurfaceView used by Camera on Android?
SurfaceView
probably does not work like a regular View
in this regard.
Instead, do the following:
- Put your
SurfaceView
inside of aFrameLayout
orRelativeLayout
in
your layout XML file, since both of
those allow stacking of widgets on
the Z-axis - Move your drawing logic
into a separate customView
class - Add an instance of the custom View
class to the layout XML file as a
child of theFrameLayout
orRelativeLayout
, but have it appear
after theSurfaceView
This will cause your custom View
class to appear to float above the SurfaceView
.
move overlay image on camera preview
if you do not use OpenGL and just float your overlay with opacity over surfaceView, you can set your own onDragListener() that handles android DragEvent( http://developer.android.com/reference/android/view/DragEvent.html) that can move the overlay detecting user's finger
Overlay image on cameraPreview
Do something like this
Create camera preview class by extends SurfaceView
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
private String TAG = "CameraPreview";
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);
}
@Override
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());
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
try {
mCamera.stopPreview();
} catch (Exception e) {
// ignore: tried to stop a non-existent preview
}
mCamera.release();
}
@Override
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
StartPreview();
}
public void StartPreview() {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e) {
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
Then in your activity
Declare Preview Object and initialize Camera Object as well
private CameraPreview preview;
After that
// Create our Preview view and set it as the content of ur activity.
preview = new CameraPreview(this, camera);
// Create Frame layout
FrameLayout previewLayout = new FrameLayout(this);
// Create camera layout params
LinearLayout.LayoutParams previewlayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, Gravity.LEFT);
// Add preview to previewLayout
previewLayout.addView(preview, 0);
Bitmap overlayBitmap = getBitmap();
if (overlayBitmap != null) {
Matrix matrix = new Matrix();
matrix.postRotate(180);
Bitmap rotatedBitmap = Bitmap.createBitmap(overlayBitmap, 0, 0, overlayBitmap.getWidth(),
overlayBitmap.getHeight(), matrix, true);
ImageView oImageView = new ImageView(this);
oImageView.setImageBitmap(rotatedBitmap);
previewLayout.addView(oImageView, 1);
}
// Add previewLayout to main layout
linearLayout.addView(previewLayout, previewlayoutParams);
That its.
Android SurfaceView overlay on camera view does not show when preview is on
Ok, if anyone else gets this, solution is to call this on the surfaceView
this.setZOrderMediaOverlay(true);
Using Surface View for camera and others for overlay image
You should be using a FrameLayout to place views on top of other views. SurfaceView first, then the views you want on top of it.
I actually did this myself, putting an image over a camera preview. I used a FrameLayout with a SurfaceView and an ImageView on top of it. Worked pretty well.
Related Topics
How to Send JSON Object to Server Using Volley in Android
How to Get the Fragment Instance from the Fragmentactivity
Android Path to Asset Txt File
Android: Determine Active Input Method from Code
Android Share Intent for a Bitmap - Is It Possible Not to Save It Prior Sharing
Recyclerview Gridlayoutmanager: How to Auto-Detect Span Count
How to Set Text Size of Textview Dynamically for Different Screens
Icon in Menu Not Showing in Android
How to Join Png with Alpha/Transparency in a Frame in Realtime
Get the Distance Between Two Geo Points
How to Add Action Bar from Support Library into Preferenceactivity
Android Textview:"Do Not Concatenate Text Displayed with Settext"
Upload Video to Facebook in Android
How to Serialize My User Class for the Firebase Database and Fix This Error
Android Communication Between Two Applications