unresolved 'main' and 'mPreview' in Android camera example at java2s.com - android

I'm trying to work a camera preview example at
http://www.java2s.com/Code/Android/Hardware/Camerapreview.htm
I copied the following code
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class PreviewActivity extends Activity implements SurfaceHolder.Callback {
Camera mCamera;
SurfaceView mPreview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView)findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
}
#Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}
#Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
mCamera.setParameters(params);
mCamera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mPreview.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PREVIEW","surfaceDestroyed");
}
and also at layout activity_man.xml
the following code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="#+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
I get errors at the following lines:
setContentView(R.layout.main);
Preview = (SurfaceView)findViewById(R.id.preview);
main and preview cannot be resolved
How can I correct this:
thanks
Dave

If name of the layout file is activity_man.xml, then you should call it right.
setContentView(R.layout.activity_man);

Related

Camera App Crash at start

Hello i'm trying to build custom camera app following the android developer guides. The code is clean with no errors, i followed the guide step to step but when i start the app it crash. i'm using the deprecated camera api.
this is the code of my own SurfaceView called CameraPreview
package guide.android.cameraappandroidguide;
import android.content.Context;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private static final String TAG = "Error";
private SurfaceHolder mHolder;
private android.hardware.Camera mCamera;
public CameraPreview(Context context,android.hardware.Camera mCamera) {
super(context);
this.mCamera = mCamera;
mHolder = getHolder();
mHolder.addCallback(this);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
Log.e("Surface creata","Ottimo");
} catch (IOException e) {
Log.e(TAG,"Error setting camera preview: " + e.getMessage());
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
if(mHolder.getSurface()==null){
return;
}
try {
mCamera.stopPreview();
}catch (Exception e){
Log.e(TAG,"Error setting camera preview: "+e.getMessage());
}
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG,"Error setting preview: "+e.getMessage());
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
This is my MainActivity
package guide.android.cameraappandroidguide;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity {
private Camera mCamera;
private CameraPreview mCameraPreview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mCamera = getCameraInstance();
mCameraPreview = new CameraPreview(this,mCamera);
FrameLayout frameLayoutPreview = (FrameLayout)findViewById(R.id.frame_layout_preview);
frameLayoutPreview.addView(mCameraPreview);
}
#Override
protected void onPause() {
super.onPause();
releaseCamera();
}
#Override
protected void onDestroy() {
super.onDestroy();
releaseCamera();
}
public static Camera getCameraInstance(){
Camera c= null;
//Proviamo ad aprire la Camera
try {
c = Camera.open();
}catch (Exception e){
Log.e("Error","Can't open th camera device");
}
return c;
};
public void releaseCamera(){
if(mCamera!=null){
mCamera.release();
mCamera=null;
}
}
}
This is my LogCat.
enter image description here
The app crash at start, i cant understand what's the problem..
Anyone can help me to understand how i can find the error using logcat or other debugging methods?
Thank you
try to add this line:
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
after this:
mHolder = getHolder();
mHolder.addCallback(this);

Custom camera class in android

I create a CameraSurfaceView.java
And i added cameraSurfaceView in containerView.
Also, i input permission and feature.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
But it doesn't work.
How do i fix this?
here is my sample code,
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/cameraPreview"
android:layout_width="350dp"
android:layout_height="350dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
FrameLayout containerView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFormat(PixelFormat.UNKNOWN);
containerView= (FrameLayout)findViewById(R.id.camerapreview);
CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(MainActivity.this);
containerView.addView(cameraSurfaceView);
}
}
CameraSurfaceView.java
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
Camera camera = null;
SurfaceHolder surfaceHolder;
boolean previewing = false;
public CameraSurfaceView(Context context) {
super(context);
surfaceHolder = getHolder();
if (surfaceHolder != null) {
surfaceHolder.addCallback(this);
}
}
#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 = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
camera.setDisplayOrientation(90);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
previewing = false;
}
Thanks.
you need to pass camera object also in CameraSurfaceView
public CameraSurfaceView(Context context,Camera camera) {
super(context);
this.camera = camera;
surfaceHolder = getHolder();
if (surfaceHolder != null) {
surfaceHolder.addCallback(this);
}
}
In you main activty you need to open the camera //initialize,
private Camera mCamera;
CameraSurfaceView cameraSurfaceView = new CameraSurfaceView(MainActivity.this,mCamera);

ImageWriter alternative for android 19-22?

In version 23 of the android API there was the introduction of the class ImageWriter.
I need to use this class in an app that should run on api 19.
How can I re-implement the class? Is there some equivalent code (I have an Image instance I need to draw to a surface)?
Here you have a code which allows you to take a picture and preview it on a Surface. I'm sure you can adapt the code for your purpose.
Here's the XML code:
<?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">
<SurfaceView
android:id="#+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dip"
android:layout_alignParentBottom="true"
android:gravity="center_vertical"
android:background="#A000">
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="Cancel"
android:onClick="onCancelClick"
/>
<Button
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Snap Photo"
android:onClick="onSnapClick"
/>
</RelativeLayout>
</RelativeLayout>
And here the Java code:
package app.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Toast;
public class PreviewActivity extends Activity implements SurfaceHolder.Callback, Camera.ShutterCallback, Camera.PictureCallback {
Camera mCamera;
SurfaceView mPreview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPreview = (SurfaceView)findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
}
#Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}
#Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
Log.d("CAMERA","Destroy");
}
public void onCancelClick(View v) {
finish();
}
public void onSnapClick(View v) {
mCamera.takePicture(this, null, null, this);
}
#Override
public void onShutter() {
Toast.makeText(this, "Click!", Toast.LENGTH_SHORT).show();
}
#Override
public void onPictureTaken(byte[] data, Camera camera) {
//Here, we chose internal storage
try {
FileOutputStream out = openFileOutput("picture.jpg", Activity.MODE_PRIVATE);
out.write(data);
out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
camera.startPreview();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters params = mCamera.getParameters();
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
Camera.Size selected = sizes.get(0);
params.setPreviewSize(selected.width,selected.height);
mCamera.setParameters(params);
mCamera.setDisplayOrientation(90);
mCamera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mPreview.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PREVIEW","surfaceDestroyed");
}
}

Open camera in android using Camera Api

I have tried all the existing methods out there.. but everytime the application force closes. Here is the code. Please help me debugging it.
MainActivity.java
package com.example.camera1;
import android.os.Bundle;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Camera cameraObject;
private ShowCamera showCamera;
private ImageView pic;
public static Camera isCameraAvailiable(){
Camera object = null;
try { object = Camera.open();
} catch (Exception e){
} return object;
}
private PictureCallback capturedIt = new PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data .length);
if(bitmap==null){
Toast.makeText(getApplicationContext(), "not taken", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), "taken", Toast.LENGTH_SHORT).show();
}
cameraObject.release();
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pic = (ImageView)findViewById(R.id.imageView1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view){
cameraObject.takePicture(null, null, capturedIt);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ShowCamera.java
package com.example.camera1;
import java.io.IOException;
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera theCamera;
public ShowCamera(Context context,Camera camera) {
super(context);
theCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
try {
theCamera.setPreviewDisplay(holder);
theCamera.startPreview();
} catch (IOException e)
{
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<?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" >
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="199dp" >
</FrameLayout>
<Button
android:id="#+id/button_capture"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick = "snapIt"
android:text="#string/Capture"/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"/>
</LinearLayout>
I am making this for android gingerbread and above versions. where my target sdk is Android Kitkat.
While testing the app on gingerbread it opens the application but simply shows a white screen that is it doesnt load the layout and force closes.. please help..!!
To open the camera,use the following code appropriately:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
Camera.CameraInfo info=new Camera.CameraInfo();
for (int i=0; i < Camera.getNumberOfCameras(); i++) {
Camera.getCameraInfo(i, info);
if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
camera=Camera.open(i);
}
}
}
if (camera == null) {
camera=Camera.open();
}
Add the SurfaceView to a FrameLayout in order to display it.
Instead of returning the Camera,you could instead use the global Camera instance variable.The camera is released in onPause like this:
mCamera.release();
mCamera=null;
//remove SurfaceView from the FrameLayout you added it to
You can retain the Camera.Parameters object and use it to set the parameters to the value by placing a check in your surfaceChanged.
You could also consider starting your Camera in a seperate Thread in onCreate as it has been done here:
//This code is from AOSP ICS:
Thread mCameraOpenThread = new Thread(new Runnable() {
public void run() {
try {
mCameraDevice = Util.openCamera(Camera.this, mCameraId);
} catch (CameraHardwareException e) {
mOpenCameraFail = true;
} catch (CameraDisabledException e) {
mCameraDisabled = true;
}
}
});
This is the onCreate method:
super.onCreate(icicle);
mCameraOpenThread.start();
SurfaceView preview = (SurfaceView) findViewById(R.id.camera_preview);
SurfaceHolder holder = preview.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// Make sure camera device is opened.
try {
mCameraOpenThread.join();
mCameraOpenThread = null;
}
catch(InterruptedException e)
{
//ignore
}
The link to the code is here http://android.googlesource.com/platform/packages/apps/Camera/+/ics-factoryrom-2-release/src/com/android/camera/Camera.java

"params cannot be resolved to a variable" while trying to get Camera Parameters

I'm new to android and I'm trying to make an app that accesses the camera with zoom and an overlay. To implement zoom I used this:
add Zoom controls of Camera in Camera
I've tried to initialize "params" using this:
Zoom In & Out Custom Camera - Null Pointer Exception
After initialising params in the constructor I still get the error "params cannot be resolved to a variable"
Have I missed something?
Any help would be greatly appreciated.
MainActivity.java
package com.reidbailey.droidscope;
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ZoomControls;
public class MainActivity extends Activity implements SurfaceHolder.Callback{
Camera camera;
int currentZoomLevel = 0, maxZoomLevel = 0;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
boolean cameraview = false;
LayoutInflater inflater = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.cameraview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
inflater = LayoutInflater.from(getBaseContext());
View view = inflater.inflate(R.layout.overlay, null);
LayoutParams layoutParamsControl= new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
this.addContentView(view, layoutParamsControl);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
if(cameraview){
camera.stopPreview();
cameraview = false;
}
if (camera != null){
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
cameraview = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ZoomControls zoomControls = (ZoomControls) findViewById(R.id.CAMERA_ZOOM_CONTROLS);
params = camera.getParameters();
if(params.isZoomSupported()){
maxZoomLevel = params.getMaxZoom();
zoomControls.setIsZoomInEnabled(true);
zoomControls.setIsZoomOutEnabled(true);
zoomControls.setOnZoomInClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel < maxZoomLevel){
currentZoomLevel++;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
zoomControls.setOnZoomOutClickListener(new OnClickListener(){
public void onClick(View v){
if(currentZoomLevel > 0){
currentZoomLevel--;
camera.startSmoothZoom(currentZoomLevel);
}
}
});
}
else
zoomControls.setVisibility(View.GONE);
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera = Camera.open();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera.release();
camera = null;
cameraview = false;
}
}
activity_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" >
<SurfaceView
android:id="#+id/cameraview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ZoomControls
android:id="#+id/CAMERA_ZOOM_CONTROLS"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
It's because params is nowhere defined in your code, hence the compiler is complaining. You can declare your variable in place where you're using it, so just add Camera.Parameters in front of your variable name:
Camera.Parameters params = camera.getParameters();
or - if you want to have it accessible in the whole MainActivity, place it below Camera declaration in MainActivity:
Camera camera;
//add params
Camera.Parameters params;
While accepted answer is correct.
As, me, if you too, where here for "cannot resolve Camera.Parameters"
You need to import proper library.
import android.hardware.Camera;
in copy paste i had imported incorrectly
import android.graphics.Camera;

Categories

Resources