2

I am developing a Camera app and I've run into a problem. More details below. I also need help with making the pictures be saved in another thread.

1. ERROR

The error I get is:

error: method addCallback in interface SurfaceHolder cannot be applied to given types;
required: Callback
found: main
reason: actual argument main cannot be converted to Callback by method invocation conversion

The code:

public class main extends Activity {

    Camera mCamera;
    SurfaceView mPreview;
    int camid = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPreview = (SurfaceView)findViewById(R.id.preview);
        mPreview.getHolder().addCallback(SurfaceHolder.Callback);
        mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mCamera = Camera.open();
    }

    // REST OF THE CODE

}

And the line the error is on:

mPreview.getHolder().addCallback(**SurfaceHolder.Callback**);

When I add "implements SurfaceHolder.Callback" to the main class and change the code bolded above to this, I get it to work, but some other parts of the code fail to use this. I hope there is some other way to fix this.

2. SAVING THE PICTURES

When a user takes a picture, the device freezes for some time until the picture is compressed and until the watermark is being added. I would like to make the app show the taken picture to the user right away and that the processing is done in another thread so it doesn't freeze.

Here is my current code:

static Camera.PictureCallback jpegcallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
    try {
        String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
        String FileName = "IMG_" + timeStamp + ".jpg";

        File saveFolder = new File(Environment.getExternalStorageDirectory() + "/App/");
        File saveFile = new File(Environment.getExternalStorageDirectory() + "/App/" + FileName);

        if(!saveFolder.exists()) {
            saveFolder.mkdir();
        }
        if(!saveFile.exists()) {
            saveFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(saveFile, false);

        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

        Bitmap workingBitmap = Bitmap.createBitmap(bitmap);
        Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);

        Canvas canvas = new Canvas(mutableBitmap);

        Paint paint = new Paint();
        paint.setColor(Color.WHITE); // Text Color
        paint.setStrokeWidth(12); // Text Size
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern

        canvas.drawBitmap(mutableBitmap, 0, 0, paint);
        canvas.drawText("Fuse", 10, 10, paint);

        mutableBitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);

        fos.close();

        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "Fuse Picture");
        values.put(MediaStore.Images.Media.DESCRIPTION, "Fuse Picture");
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(MediaStore.Images.ImageColumns.BUCKET_ID, saveFile.toString().toLowerCase(Locale.US).hashCode());
        values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, saveFile.getName().toLowerCase(Locale.US));
        values.put("_data", saveFile.getAbsolutePath());

        /*ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);*/
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    }
    camera.startPreview();
}
};

Can anybody please try to make it process the picture in another thread and give me code to show the image instantly on an ImageView. Also, there is one commented part with ContentResolver and when I uncomment that code, it gives me error on this part getContentResolver(). I found that code over here. The error is: non-static method getContentResolver() cannot be referenced from a static context.

I am sorry for asking so much but I'm pretty much new to Android development.

Thanks in advance!

Community
  • 1
  • 1
leongrdic
  • 80
  • 11

1 Answers1

4

I hope you have found your answer. But either way I was having a similar problem and I figured it out, and I ended up here when searching for a solution.

The solution is to add the implements to the class

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback 
    {...}

Source Article

CodePhobia
  • 1,315
  • 10
  • 19