Hery Irawan wrote:
> Hi, I want to create motion detection app, but I still have a problem
> with my code
>
You have many problems with your code. I think you're getting
'NullPointerException' (NPE). Do you see this in your "logcat"?
As TreKing has told you (twice!) you must tell us what is wrong. "I have a
problem" is a very useless way to ask a question.
What *precisely* is the behavior you experience, and how *exactly* does it
differ from the *exact* behaviors you expect?
*Copy and paste* and error message into your report (such as the NPE
messages you surely are getting).
What have you done to diagnose your problem? (Writing to a programming
forum doesn't count.)
> Here my code:
>
>
> package com.iseng;
>
> import java.io.File;
> import java.io.FileNotFoundException;
> import java.io.FileOutputStream;
> import java.io.IOException;
> import java.io.OutputStream;
>
> import android.app.Activity;
> import android.content.ContentValues;
> import android.content.pm.ActivityInfo;
> import android.content.res.Configuration;
> import android.net.Uri;
> import android.os.Bundle;
> import android.os.Environment;
> import android.provider.MediaStore.Images.Media;
> import android.util.Log;
> import android.view.SurfaceHolder;
> import android.view.SurfaceView;
> import android.widget.Toast;
> import android.hardware.*;
> import android.hardware.Camera.PictureCallback;
>
> public class IsengActivity extends Activity implements
> SurfaceHolder.Callback, Camera.PreviewCallback, PictureCallback {
>
>
Please be consistent and conventional with indentation. Use spaces (not
TABs) to indent, up to four (though two works well) per level for forum
posts.
> Camera kamera;
> SurfaceHolder surface;
> SurfaceView kameraview;
>
The Java naming convention would suggest 'kameraView' as the variable name.
> public byte dataOld[];
>
This is 'null' to start with (hence your NPEs) and should not be 'public'.
> public void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.main);
>
> setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
> kameraview =(SurfaceView) findViewById(R.id. kameraku);
> surface = kameraview.getHolder();
> surface.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
> surface.addCallback(this);
> }
> @Override
> public void onPreviewFrame(byte[] data, Camera kamera) {
>
> if(data.length ==0) return;
What if 'data == null'?
> if(dataOld.length==0 )
'dataOld' certainly is 'null' the first time.
NPE!
> data=dataOld;
You forgot the curly braces around the 'if' body.
If 'dataOld' is not 'null' and is empty, it will wipe out 'data' at this
point. Is that what you want? The logic is not obvious and should be
commented.
> int Isdiferent=0;
Please follow the Java naming conventions: 'isDifferent'.
> int treshold =50;
Java naming conventions would suggest 'threshold' for this variable name.
> for (int x=0; x<data.length; x=+200)
Well, if somehow 'dataOld' was not 'null' and was empty, you got nothing
here.
> {
> if(Math.abs(data[x] - dataOld[x]) <= treshold )
> {
> Isdiferent +=1;
You could also use the unary increment operator here.
> }
>
> float precentage = Isdiferent / data.length;
The Java naming conventions would suggest that this variable name be
'percentage'.
> if (precentage > 20 )
You should use '20.0' since you're comparing floating-point values.
> {
> kamera.takePicture(null,null,this);
> }
>
> }
> }
>
> @Override
> public void surfaceChanged(SurfaceHolder holder, int format, int
h, int w) {
> kamera.startPreview();
> }
>
> @Override
> public void surfaceCreated(SurfaceHolder holder) {
> kamera=Camera.open();
>
> try
> {
> Camera.Parameters parameter =
kamera.getParameters();
Naming conventions would suggest that this variable be named in the plural.
>
if(this.getResources().getConfiguration().orientation !=
'this' in front of a method call is just silliness.
> Configuration.ORIENTATION_LANDSCAPE)
> {
> parameter.set("orientation","portrait");
> kamera.setDisplayOrientation(90);
> }
>
> else
> {
>
parameter.set("orientation","landscape");
> kamera.setDisplayOrientation(0);
> }
>
> kamera.setParameters(parameter);
> kamera.setPreviewDisplay(holder);
> }
>
> catch(IOException e)
> {
> kamera.release();
Is this the best way to handle this exception?
>
> }
> kamera.startPreview();
> }
>
> @Override
> public void surfaceDestroyed(SurfaceHolder holder) {
> kamera.stopPreview();
> kamera.release();
>
> }
>
> @Override
> public void onPictureTaken(byte[] data, Camera kamera) {
What if either parameter is 'null'?
>
> // TODO Auto-generated method stub
You should remove these auto-generated comments.
> Uri imageFileUri =
> getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,new
> ContentValues());
> try
>
> {
> /*String sdcardStorage =
> Environment.getExternalStorageDirectory().toString();
>
imageFileUri=Uri.parse(sdcardStorage + "/HeryMD/Test.jpg");*/
Don't include superfluous comments in forum posts.
> String sdcardStorage =
> Environment.getExternalStorageDirectory().toString();
> File f = new File(sdcardStorage
+ "/HeryMD/Test.jpg");
> OutputStream os = new
FileOutputStream(f);
> os.write(data);
> os.flush();
> os.close();
You need to guarantee that the 'flush()' and 'close()' calls occur, which
you have not done.
> } catch(FileNotFoundException e)
> {
> Toast t =
Toast.makeText(this,e.getMessage(),
> Toast.LENGTH_SHORT);
> t.show();
> }
>
> catch(IOException e)
> {
> Toast t =
Toast.makeText(this,e.getMessage(),
>Toast.LENGTH_SHORT);
> t.show();
> }
>
> kamera.startPreview();
> }
> }
--
Lew
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en