I have some frames in my SD card.
There is list of frames in my activity.
Now when user click on frame, Camera preview will be displayed through that frame.
Please see image.
How can I archive this functionality ?
I searched about this but didn't get proper example.
You have to create your own Preview by extending the SurfaceView class.
Take FrameLayout with SurfaceView as child.and customise as per your needs
You can use SurfaceView and create a CustomView that will open the camera and you can adjust its size in the xml accordingly.
Create a class that extends SurfaceView and open camera inside that
CustomPreview.java
public class CustomPreview extends SurfaceView implements SurfaceHolder.Callback{
public static Bitmap mBitmap;
SurfaceHolder holder;
static Camera mCamera;
public CustomPreview(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.getSupportedPreviewSizes();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
/***
*
* Take a picture and and convert it from bytes[] to Bitmap.
*
*/
public static void takeAPicture(){
Camera.PictureCallback mPictureCallback = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
};
mCamera.takePicture(null, null, mPictureCallback);
}
}
Now you can add this custom view in your XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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/mySurfaceView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.mjl.CustomPreview
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</com.mjl.CustomPreview>
</FrameLayout>
<LinearLayout
android:layout_below="#id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center">
<ImageView android:id="#+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"/>
</LinearLayout>
</RelativeLayout>
Now Enjoy the output.
Related
i need to capture image from required portion of the screen.
capture image from camera.
at that time other screen content as it is.
how is this possible?
try to use Surface View for creating dynamic camera view and set in your required portion.
following code try
variables set Class level (Global)
Button btn_capture;
Camera camera1;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
public static boolean previewing = false;
Following code in onCreate() method
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = new SurfaceView(this);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btn_capture = (Button) findViewById(R.id.button1);
surfaceView.setBackgroundResource(R.drawable.your_background_image);
if(!previewing){
camera1 = Camera.open();
if (camera1 != null){
try {
camera1.setDisplayOrientation(90);
camera1.setPreviewDisplay(surfaceHolder);
camera1.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
btn_capture.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(camera != null)
{
camera1.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
}
}
});
Following code put after onCreate() in your class.
ShutterCallback myShutterCallback = new ShutterCallback(){
public void onShutter() {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
public void onPictureTaken(byte[] arg0, Camera arg1) {
// TODO Auto-generated method stub
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Bitmap correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);
}};
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(previewing){
camera1.stopPreview();
previewing = false;
}
if (camera1 != null){
try {
camera1.setPreviewDisplay(surfaceHolder);
camera1.startPreview();
previewing = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera1.stopPreview();
camera1.release();
camera1 = null;
previewing = false;
}
in AndroidManifest.xml give user-permissions.
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
and also not forgot ( implements SurfaceHolder.Callback ) to the class.
I already created like that kind of Camera. What I did is, I covered the other area of the camera with an image, and cut the center part of the image and save it as png file, to make the center transparent.
You will set background image of your frame(camera preview) with that image. So that it will look like the camera is only that portion that is transparent or the circle.
I used this tutorial to open,create preview,and take picture from camera device
http://developer.android.com/guide/topics/media/camera.html
in this part(u can see this in the link I provide above)
private PictureCallback mPicture = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//this is where you crop your image
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inMutable = true;
Bitmap bitmap = BitmapFactory
.decodeByteArray(data, 0, data.length, opt);
bitmap=Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
Canvas mcanvas=new Canvas(bitmap);
//do the cropping here, bitmap is the image you will use to crop
}
}
follow this tutorial on how to crop the image to circle
Cropping circular area from bitmap in Android
you can use surface view.after capture image u can get in bitmap and draw canvas also
http://developer.android.com/reference/android/view/View.html#onDraw(android.graphics.Canvas)
http://developer.android.com/reference/android/view/SurfaceView.html#setZOrderMediaOverlay(boolean)
If the part of the screen is actually a view, you can capture only this view. Like this:
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
If you want to capture only small portion of a view, you have to calculate rectangle of this side. Then:
Bitmap bitmap = Bitmap.createBitmap(rect.width(),rect.height(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.save();
canvas.translate(-rect.left,-rect.top);
view.draw(canvas);
canvas.restore();
It's only a pseudocode, but I hope you get the idea. Simply translate and draw only the part you need.
I use the CameraPreview in ApiDemos application and edit it as your requirement.
First, copy the code of the Preview class into a new class file in the same package so that it is public and you can declare it in the xml layout file. Remember to add one more constructor as below:
public Preview(Context context, AttributeSet attrs) {
super(context, attrs);
mSurfaceView = new SurfaceView(context);
addView(mSurfaceView);
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = mSurfaceView.getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
Sample layout file with sample width and height:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Abow"/>
<com.example.android.apis.graphics.Preview
android:id="#+id/camera_view"
android:layout_width="240dp"
android:layout_height="180dp">
</com.example.android.apis.graphics.Preview>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Below"/>
</LinearLayout>
In the onCreate() method of the CameraPreview activity, change the setContentView part as followed:
setContentView(R.layout.camera_layout);
mPreview = (Preview) findViewById(R.id.camera_view);
use TextureView for preview,set layout_width and layout_height what ever you want.
here is the code:
public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
private Camera mCamera;
private TextureView mTextureView;
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextureView = (TextureView) findViewById(R.id.textureView);
mTextureView.setSurfaceTextureListener(this);
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
mCamera = Camera.open();
try {
mCamera.setPreviewTexture(surfaceTexture);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
} catch (IOException exception) {
}
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
//To change body of implemented methods use File | Settings | File Templates.
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
mCamera.startPreview();
mCamera.release();
return true;
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
and xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextureView
android:layout_gravity="center"
android:id="#+id/textureView"
android:layout_width="200dp"
android:layout_height="300dp"/>
</LinearLayout>
Actually I don't understand how to implement the rectangle on the camera preview. Everything working well, but I don't know the way to put the rectangle border like this link. Please anyone help me. Thanks.
I suggest you implement your camera preview in a custom SurfaceView and then in your XML you can simply overlay the rectangle, like the accepted answer here:
Android: Overlay on Android Camera Preview
So add a Java file to your project:
public class CapturePreview extends SurfaceView implements SurfaceHolder.Callback{
public static Bitmap mBitmap;
SurfaceHolder holder;
static Camera mCamera;
public CapturePreview(Context context, AttributeSet attrs) {
super(context, attrs);
holder = getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
Camera.Parameters parameters = mCamera.getParameters();
parameters.getSupportedPreviewSizes();
mCamera.setParameters(parameters);
mCamera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera = Camera.open();
mCamera.setPreviewDisplay(holder);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
}
public static void takeAPicture(){
Camera.PictureCallback mPictureCallback = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
}
};
mCamera.takePicture(null, null, mPictureCallback);
}
}
Please note you will have to make sure it has the appropriate package name.
Then change your main XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<[PACKAGE_NAME].CapturePreview
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background:"#55999999"
android:padding:"30dp"
android:gravity="center">
</LinearLayout>
</RelativeLayout>
This should have a transparent rectangle over the camera preview.
This is my XML file. I take one LinearLayout in which i take SurfaceView and Button, but camera displays view like following Image... look at Image and please give me solution.
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<SurfaceView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/surface"
android:layout_weight="2"/>
<Button
android:layout_height="100dp"
android:layout_width="match_parent"
android:id="#+id/click"
android:text="Click Photo"
android:layout_weight="1"/>
</LinearLayout>
and here is my java code please check this also
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clickphotobtn=(Button)findViewById(R.id.click);
surfaceview=(SurfaceView)findViewById(R.id.surface);
surfaceholder=surfaceview.getHolder();
surfaceholder.addCallback(this);
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
clickphotobtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//startActivity(intent);
camera.takePicture(null, null, null);
}
});
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
camera = Camera.open();
camera.setPreviewDisplay(surfaceholder);
// Toast.makeText(getApplication(), "Create", Toast.LENGTH_LONG).show();
} catch (IOException e) { }
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
Camera.Parameters parameters = camera.getParameters();
Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation()== Surface.ROTATION_0)
{
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(90);
}
if(display.getRotation()== Surface.ROTATION_90 || display.getRotation()== Surface.ROTATION_180)
parameters.setPreviewSize(width, height);
if(display.getRotation()==Surface.ROTATION_270)
{
parameters.setPreviewSize(width, height);
camera.setDisplayOrientation(180);
}
// camera.setParameters(parameters);
try{
camera.setPreviewDisplay(surfaceholder);
camera.startPreview();
}
catch (Exception e){
Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (null == camera)
return;
camera.stopPreview();
camera.release();
camera = null;
preview = false;
//Toast.makeText(getApplication(), "Destroy", Toast.LENGTH_LONG).show();
}
This is the right way to change camera orientation, this piece of code worked for me. camera.setDisplayOrientation(90);
Try to follow the way suggested by the documentation, using Camera.CameraInfo in order to obtain the camera orientation and updating it consequently.
Look at the example here: void setDisplayOrientation(int).
Add
public class YourActivity extends AppCompatActivity implements SurfaceHolder.Callback{
....
}
I want add a take picture function in lockscreen , and I need to open the preview on screen , but it's fail all the time , logcat say the Error is about "setPreviewWindow - Null ANativeWindow passed to setPreviewWindow" and "startPreview - Preview not started. Preview in progress flag set"
In keyguard_screen_tab_unlock.xml after the digital clock view, put
<FrameLayout
android:id="#+id/mPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
In LockScreen.java constructor:
FrameLayout mFrameLayout = (FrameLayout)findViewById(R.id.mPreview);
SurfacePreview preview = new SurfacePreview(mContext);
preview.setZOrderOnTop(true);
mFrameLayout.addView(preview);
SurfacePreview class :
public class SurfacePreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mSurfaceHolder;
SurfacePreview(Context context) {
super(context);
mSurfaceHolder = this.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
e.printStackTrace();
}
mCamera.startPreview();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
}
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.release();
}
}
Anyone kown how to resolve this issue and start prview on LockScreen? Thanks a lot.
I am using Camera feature in my app. Everything works fine but when the device gets locked/sleep, upon returning to the app the camera portion(SurfaceView) is just black. Below is my code. Can someone please identify the problem?
public class Activity_Camera extends Activity implements SurfaceHolder.Callback, Camera.AutoFocusCallback, Observer
{
// Surface vars
private SurfaceView preview;
private SurfaceHolder previewHolder;
// Camera vars
private Camera mCamera;
private boolean mPreviewRunning = false;
private boolean mCaptureFrame = false;
private int frame_number = 1;
private byte[] frame = new byte[1];
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.addCallback(this);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
#Override
public void onPause()
{
super.onPause();
if (mPreviewRunning)
{
mCamera.stopPreview();
mPreviewRunning = false;
}
mCamera.setPreviewCallback(null);
mCamera.release();
}
// implements SurfaceHolder.Callback
public void surfaceCreated(SurfaceHolder holder)
{
mCamera = Camera.open();
}
// implements SurfaceHolder.Callback
public void surfaceDestroyed(SurfaceHolder holder)
{
mPreviewRunning = false;
}
// implements Camera.AutoFocusCallback
public void onAutoFocus(boolean success, Camera camera)
{
}
PreviewCallback previewCallback = new PreviewCallback()
{
public void onPreviewFrame(byte[] data, Camera camera)
{
}
};
// implements SurfaceHolder.Callback
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
if (mPreviewRunning)
{
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(640, 480);
mCamera.setParameters(p);
try
{
mCamera.setPreviewDisplay(holder);
} catch (IOException e)
{
e.printStackTrace();
}
mCamera.setPreviewCallback(previewCallback);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0:
degrees = 90;
break;
case Surface.ROTATION_90:
degrees = 0;
break;
case Surface.ROTATION_180:
degrees = 270;
break;
case Surface.ROTATION_270:
degrees = 180;
break;
}
Log.i("DEGREES ARE WHAT??", Integer.toString(degrees));
// mCamera.setDisplayOrientation(degrees);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
mPreviewRunning = true;
}
// implements Observer
// captures a frame when the compass listener says it is appropriate
public void update(Observable observable, Object data)
{
mCaptureFrame = true;
}
}
Please identify the issue, as I suspect something wrong with onPause(), it also gives me exception when I press back button after the black camera screen:
java.lang.RuntimeException: Method called after release()
on this line:
mCamera.setPreviewCallback(null); // (in onPause())
Here is the XML:
<?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"
android:background="#E6E5E6">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:id="#+id/tv_camera_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textColor="#android:color/black"
android:text="URL goes here........"
android:layout_marginTop="3dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/tv_speaknow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:background="#ffffff"
android:layout_marginTop="3dp"
android:text="Speak Now"
android:textColor="#ff0000"
android:textStyle="bold"/>
</LinearLayout>
<RelativeLayout
android:id="#+id/rl_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:id="#+id/preview"
android:layout_width="480px"
android:layout_height="640px"
android:layout_alignParentBottom="true"/>
<Button
android:id="#+id/btn_take_picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/camera"
android:layout_alignBottom="#+id/preview"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp"/>
</RelativeLayout>
</LinearLayout>
If its the first time you are launching the app, the onPause really shouldn't be the problem.
But I'd move down the creation and such to onResume instead of onCreate.
And can i see your xml of the surfaceview?
Do you just want 1 picture?
if (mPreviewRunning)
{
mCamera.stopPreview();
}
And most people startPreview from onResume. try to move it up there..