Reputation: 3564
INTRODUCTION
I have a custom camera app, where I show camera preview over a custom surfaceView. What I need to implement is, create an overlay image from an image resource, that will be shown over the cameraPreview, such as a filter.
I'm been searching about this in different topics but I haven't found something aplicable to my app.
APPROACH
I've been trying to set the image as bitamp, using onDraw() method and other options, but I haven't achieved to show the image.
This is my code:
CODE
public class CameraActivity extends Activity implements PictureCallback {
private Button btnPhoto;
CameraPreview cameraPreview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cameraPreview = (CameraPreview) findViewById(R.id.camera_preview);
btnPhoto = (Button)findViewById(R.id.buttonTakePhoto);
btnPhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
takePhoto();
}
});
}
public void takePhoto() {
cameraPreview.getCamera().takePicture(null, null, this);
}
//...
}
_
public class CameraPreview extends FrameLayout implements SurfaceHolder.Callback {
private SurfaceView surfaceView;
private Camera camera;
public CameraPreview(Context context, AttributeSet attrs) {
super(context, attrs);
createCamera();
surfaceView = new SurfaceView(context);
addView(surfaceView);
SurfaceHolder holder = surfaceView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.setKeepScreenOn(true);
}
private void createCamera() {
//...
try {
this.camera = Camera.open(activeCameraId);
} catch (Exception e) {
return;
}
//...
}
public Camera getCamera() {
return camera;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//...
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//...
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//...
}
}
_
<com.uax.cameratakephoto.CameraPreview
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true" />
<Button //THIS IS THE BUTTON TO TAKE THE PHOTO
android:id="@+id/buttonTakePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@android:drawable/ic_menu_camera" />
<ImageView //THIS IS A SMALL PREVIEW OF THE LAST PHOTO TAKEN
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
Upvotes: 3
Views: 10517
Reputation: 6373
I did the same thing, but with a TextView over a camera preview. The trick was dettaching it from the XML Layout and attaching it over the Camera´s parent.
The answer is not intuitive at all, but I found how to do it. Unlike Linear Layouts, order of declaration does NOT define the Z order in Relative Layouts. I was able of overlaying a textview over the Camera Preview by declaring both views in XML and, and overlaying them programatically on my Activity's onCreate Method.
Suppose you have an XML with a TextView with a nice transparent backgroud that you want to overlay over the Camera Preview frame layout, because why not?, it looks cool!:
<RelativeLayout>
<TextView
android:id="@+id/txtnombotiga"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ola ke ase"
android:textColor="#000000"
android:textSize="16sp"
android:background="#55ffffff"
/>
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="240dp">
</FrameLayout>
</RelativeLayout>
If left like that, the camera will cover your text, no matter what :( To solve it, let´s go programatical, and: 1) detach the TextView from its parent Layout 2) attach it to the camera´s frame layout after attaching the camera first.
Heres the code
OnCreate(){ ...blaa blaaa...
//Create camera instance and reference to our frame Layout
mPreview = new CameraPreview(this, mCamera, previewCb, autoFocusCB);
FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
//Add camera to the Frame Layout
preview.addView(mPreview);
//Get reference to the TextView you want to overlay and type something funny
TextView txt=(TextView)findViewById(R.id.txtnombotiga);
txt.setText("ola keee aseee!");
//1) Step 1, here comes the magic! dettach the textView from its original Layout
((ViewGroup)txt.getParent()).removeView(txt);
//1) Step 2, voila! attach it to the View, order matters here so it will appear on
//top of the camera!
preview.addView(txt);
This is the general way to do it, If you need more details let me know. I need to meet deadlines at work, so I use the first solution that comes up to my mind, sometimes not the most elegant or efficient, if you know a better way of doing it, please share it with us!
Upvotes: 1
Reputation: 1039
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.
Upvotes: 2