Saving thumbnails after shooting in Android

I have an application that can take a snapshot using the full code below. My question is how to either access the thumbnail stored in jpeg or create a new sketch. Either put the code in this class, or a separate class that checks the folder based on an event or schedule and generates thumbnails in order.

In particular, I installed a thumbnail for it, but in any case I can’t access it. I tried to implement ExifInterface, and also tried to manually resize the photo. I was able to put other parameters in Jpeg Exif (put some random numbers for latitude and longitude). Viewing the file confirmed that the numbers were written. Many thanks.

    public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) {
        Camera.Parameters parameters=camera.getParameters();

        parameters.setPreviewSize(width, height);
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.setJpegThumbnailQuality(50);
        parameters.setJpegThumbnailSize(192, 256);

        camera.setParameters(parameters);
        camera.startPreview();
    }

This is what I tried and failed using the Exif interface:

                ExifInterface myEI = new ExifInterface(photo.getPath());
                byte[] thumbArray = myEI.getThumbnail();

                File thumbFolder = new File(appFolder.getPath(), "thumbnails");
                if (!thumbFolder.exists())
                {
                    thumbFolder.mkdirs();
                }
                File thumbnail=new File(thumbFolder, picFile.getName());
                if (thumbnail.exists()) {
                    thumbnail.delete();
                }

                Bitmap bitmap = BitmapFactory.decodeByteArray(thumbArray, 0, thumbArray.length);
                FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE);
                bitmap.compress(CompressFormat.JPEG, 50, fos);
                fos.close();

.

public class PictureTaker extends Activity {
private static final String TAG = "PictureTaker";
private SurfaceView preview=null;
private SurfaceHolder previewHolder=null;
private Camera camera=null;
String filename;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);
    preview=(SurfaceView)findViewById(R.id.preview);
    previewHolder=preview.getHolder();
    previewHolder.addCallback(surfaceCallback);
    previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode==82 || keyCode==KeyEvent.KEYCODE_SEARCH) {
        takePicture();
        return(true);
    }
    return super.onKeyDown(keyCode, event);
}

private void exitCamera() {
    finish();
    super.onStop();
}

private void takePicture() {
    camera.takePicture(null, null, photoCallback);
}

SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
    public void surfaceCreated(SurfaceHolder holder) {
        camera=Camera.open();

        try {
            camera.setPreviewDisplay(previewHolder);
        }
        catch (Throwable t) {
            Log.d(TAG, "Exception in setPreviewDisplay()", t);
        }
    }

    public void surfaceChanged(SurfaceHolder holder,int format, int width, int height) {
        Camera.Parameters parameters=camera.getParameters();

        parameters.setPreviewSize(width, height);
        parameters.setPictureFormat(PixelFormat.JPEG);
        parameters.setJpegThumbnailQuality(50);
        parameters.setJpegThumbnailSize(192, 256);

        camera.setParameters(parameters);
        camera.startPreview();
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        camera.stopPreview();
        camera.release();
        camera=null;
    }
};

Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        new SavePhotoTask().execute(data);
        camera.startPreview();
    }
};

class SavePhotoTask extends AsyncTask<byte[], String, String> {
    @Override
    protected String doInBackground(byte[]... jpeg) {
        filename = Utilities.getTimeString() + ".jpg";

        File photo=new File(filename);
        if (photo.exists()) {
            photo.delete();
        }
        try {
            FileOutputStream fos=new FileOutputStream(photo.getPath());
            fos.write(jpeg[0]);
            fos.close();

        }
        catch (java.io.IOException e) {
            Log.d(TAG, "Exception in photoCallback", e);
        }
        return(null);
    }
}

}

+3
2

, , , , , FileOutputStream . ,

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE);
FileOutputStream fos=new FileOutputStream(photo.getPath());

:

FileOutputStream fos = openFileOutput(thumbnail, MODE_WORLD_READABLE);
FileOutputStream fos=new FileOutputStream(photo);

, , , , , .

+1

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE);
FileOutputStream fos=new FileOutputStream(photo.getPath());

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE);
FileOutputStream fos=new FileOutputStream(new File(photo.getPath()));
0

Source: https://habr.com/ru/post/1786281/


All Articles