Surface cameras
Surface has not just one, but two integrated LifeCam cameras. You can use the front camera to have a video chat with a friend, or the rear-facing camera to record meetings and events hands-free. You can also use either camera to take photos.A privacy light appears on Surface when either camera is active, so there are no surprises. Both cameras are fixed focus, so you don’t need to worry about focusing. The cameras capture video in 720p HD, with a 16:9 aspect ratio.
Both cameras take photos and record videos. The rear camera is angled to point straight ahead when Surface is resting on its kickstand or when you hold your Surface at a viewing angle that’s comfortable for your wrists.
The front camera points up at your face for optimal video calling.
Surface cameras FAQ
Here are answers to some common questions about using the two Surface cameras and the built-in Camera app.
Following class for Custom CameraView.
CameraOnSurfaceProActivity.java
package com.CameraOnSurfacePro;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class CameraOnSurfaceProActivity extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
byte[] bArray;
Uri outputFileUri;
Bitmap correctBmp;
LinearLayout ll_flipcamera;
static Boolean camera_bool = false;
Bitmap bitmapPicture;
Bitmap scaled;
Bitmap newTemp;
public static int noOfCamera;
public static boolean enableFrontCamera = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
noOfCamera = Camera.getNumberOfCameras();
ll_flipcamera = (LinearLayout) findViewById(R.id.ll_flipcamera);
if(noOfCamera > 1)
{
enableFrontCamera=true;
}
else
{
ll_flipcamera.setVisibility(View.GONE);
}
Button capture = (Button)findViewById(R.id.capture);
Button flip_camera = (Button) findViewById(R.id.flip_camera);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
flip_camera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(camera_bool){
camera.release();
camera = null;
previewing = false;
camera = Camera.open(0);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
camera_bool = false;
}else{
camera.release();
camera = null;
previewing = false;
camera = Camera.open(1);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
camera_bool = true;
}
}
});
capture.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
if(camera != null)
{
camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
// System.gc();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
startCamera();
}
public void startCamera() {
if(!previewing){
camera = Camera.open(0);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
bArray = arg0;
bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Matrix mat = new Matrix();
mat.postRotate(90);
correctBmp = null;
correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);
Bitmap correctBmp1 = correctBmp;
OutputStream os = null;
try
{
File root = new File(Environment.getExternalStorageDirectory() + "/CDD/");
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicOriginal.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
os = new FileOutputStream(sdImageMainDirectory);
os.write(arg0);
// correctBmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
}
catch (Exception e)
{
Toast.makeText(CameraOnSurfaceProActivity.this,
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
scaled = cropBitmap(correctBmp1);
OutputStream fOut = null;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ "/CDD/");
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(CameraOnSurfaceProActivity.this,
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
scaled.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
fOut.write(byteArray);
// scaled.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
scaled.recycle();
correctBmp.recycle();
} catch (Exception e) {
}
}};
private Bitmap cropBitmap(Bitmap originalBitmap)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
originalBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bitmap bmp2 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Bitmap bmOverlay = Bitmap.createBitmap(276, 276, Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmp2, 100, 100, null);
c.drawRect(0, 0, 276, 276, p);
return bmOverlay;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
main.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" >
<LinearLayout
android:id="@+id/ll_flipcamera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.95"
android:background="#2e2e2e"
android:orientation="vertical" >
<Button
android:id="@+id/flip_camera"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:background="@drawable/flip_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.10"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.90"
android:background="#2e2e2e"
android:orientation="vertical" >
<Button
android:id="@+id/capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/capture_image111" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
- How do I get started using the cameras?
- Why can I take photos but not videos (or the reverse)?
- Is the camera facing the wrong way?
- Why does the camera angle seem wrong?
- How do I turn off the camera click?
- Can I use both cameras at the same time?
- How do I turn off the camera privacy light?
Following class for Custom CameraView.
CameraOnSurfaceProActivity.java
package com.CameraOnSurfacePro;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuffXfermode;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class CameraOnSurfaceProActivity extends Activity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean previewing = false;
byte[] bArray;
Uri outputFileUri;
Bitmap correctBmp;
LinearLayout ll_flipcamera;
static Boolean camera_bool = false;
Bitmap bitmapPicture;
Bitmap scaled;
Bitmap newTemp;
public static int noOfCamera;
public static boolean enableFrontCamera = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
noOfCamera = Camera.getNumberOfCameras();
ll_flipcamera = (LinearLayout) findViewById(R.id.ll_flipcamera);
if(noOfCamera > 1)
{
enableFrontCamera=true;
}
else
{
ll_flipcamera.setVisibility(View.GONE);
}
Button capture = (Button)findViewById(R.id.capture);
Button flip_camera = (Button) findViewById(R.id.flip_camera);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
flip_camera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(camera_bool){
camera.release();
camera = null;
previewing = false;
camera = Camera.open(0);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
camera_bool = false;
}else{
camera.release();
camera = null;
previewing = false;
camera = Camera.open(1);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
camera_bool = true;
}
}
});
capture.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v) {
if(camera != null)
{
camera.takePicture(myShutterCallback, myPictureCallback_RAW, myPictureCallback_JPG);
// System.gc();
}
}
});
}
@Override
protected void onResume() {
super.onResume();
startCamera();
}
public void startCamera() {
if(!previewing){
camera = Camera.open(0);
if (camera != null){
try {
camera.setDisplayOrientation(90);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ShutterCallback myShutterCallback = new ShutterCallback(){
@Override
public void onShutter() {
}};
PictureCallback myPictureCallback_RAW = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
}};
PictureCallback myPictureCallback_JPG = new PictureCallback(){
@Override
public void onPictureTaken(byte[] arg0, Camera arg1) {
bArray = arg0;
bitmapPicture = BitmapFactory.decodeByteArray(arg0, 0, arg0.length);
Matrix mat = new Matrix();
mat.postRotate(90);
correctBmp = null;
correctBmp = Bitmap.createBitmap(bitmapPicture, 0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight(), null, true);
Bitmap correctBmp1 = correctBmp;
OutputStream os = null;
try
{
File root = new File(Environment.getExternalStorageDirectory() + "/CDD/");
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicOriginal.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
os = new FileOutputStream(sdImageMainDirectory);
os.write(arg0);
// correctBmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
}
catch (Exception e)
{
Toast.makeText(CameraOnSurfaceProActivity.this,
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
scaled = cropBitmap(correctBmp1);
OutputStream fOut = null;
try {
File root = new File(Environment.getExternalStorageDirectory()
+ "/CDD/");
root.mkdirs();
File sdImageMainDirectory = new File(root, "myPicName.jpg");
outputFileUri = Uri.fromFile(sdImageMainDirectory);
fOut = new FileOutputStream(sdImageMainDirectory);
} catch (Exception e) {
Toast.makeText(CameraOnSurfaceProActivity.this,
"Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
scaled.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
fOut.write(byteArray);
// scaled.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
scaled.recycle();
correctBmp.recycle();
} catch (Exception e) {
}
}};
private Bitmap cropBitmap(Bitmap originalBitmap)
{
ByteArrayOutputStream stream = new ByteArrayOutputStream();
originalBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bitmap bmp2 = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Bitmap bmOverlay = Bitmap.createBitmap(276, 276, Bitmap.Config.ARGB_8888);
Paint p = new Paint();
p.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
Canvas c = new Canvas(bmOverlay);
c.drawBitmap(bmp2, 100, 100, null);
c.drawRect(0, 0, 276, 276, p);
return bmOverlay;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if(previewing){
camera.stopPreview();
previewing = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
previewing = false;
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
}
this capture button you have to use.
button of flip CameraView
main.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" >
<LinearLayout
android:id="@+id/ll_flipcamera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.95"
android:background="#2e2e2e"
android:orientation="vertical" >
<Button
android:id="@+id/flip_camera"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|right"
android:background="@drawable/flip_camera" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.05"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.10"
android:orientation="vertical" >
<SurfaceView
android:id="@+id/surfaceview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.90"
android:background="#2e2e2e"
android:orientation="vertical" >
<Button
android:id="@+id/capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@drawable/capture_image111" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Click here to Download SourceCode.
No comments:
Post a Comment