I am new in android development. My app should activate the camera to scan a QR code and decode it to a string. This works fine, but I have one more requirement: to present
I faced the same problem. The solution is simply add the CameraPrevie in your xml inside a frameLayout, and just after that the image you want to put on top of is.
eg.:
<FrameLayout
android:id="@+id/captionFrame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:layout_marginTop="50dp" >
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:paddingLeft="-5dp" >
</FrameLayout>
<ImageView
android:id="@+id/watermark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:src="@drawable/watermark" />
</FrameLayout>
and populate it from your activity:
Camera mCamera = getCameraInstance();
ImageScanner scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 3);
scanner.setConfig(0, Config.Y_DENSITY, 3);
CameraPreview mPreview = new CameraPreview(this, mCamera, null, autoFocusCallB);
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
preview.addView(mPreview);
I think it's easier then you thought:
In the example code provided by the ZBar guy there are those two lines in the onCreate() method:
FrameLayout preview = (FrameLayout) findViewById(R.id.cameraPreview);
preview.addView(mPreview);
which are responsible for adding the "preview view" to the camerPreview Layout.
Find the mentioned lines in your code, create a new ImageView and also add it to the preview-Layout like this:
ImageView v = new ImageView(this);
v.setImageResource(R.drawable.icon);
preview.addView(v);
Adjust the size and the position of your view v and you are done!
To present a frame in cameraview, add an imageview in the framelayout(camerapreview) in main.xml .
<FrameLayout
android:id="@+id/cameraPreview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:alpha="0.6"
android:src="@drawable/overlay" />
</FrameLayout>
Use this code to make the frame(imageview) visible. As the frame(imageview) is already present in the framelayout in main.xml we need to remove it first.
FrameLayout preview = (FrameLayout)findViewById(R.id.cameraPreview);
ImageView iv=(ImageView) findViewById(R.id.imageView1);
preview.addView(mPreview);
preview.removeView(iv);
preview.addView(iv);
To scan the camerapreview only from your frame you need to use setCrop().Adjust the size of camerapreview according to the frame.
barcode.setData(data);
//this is in portrait mode
//barcode.setCrop(left,top,width,height).
barcode.setCrop(100,230,600,10);
This worked for me and hope it will solve your problem too. Please excuse me for my grammatical mistakes. Thank you.