Android 拍照后保存缩略图

发布于 2024-10-12 09:40:03 字数 4264 浏览 7 评论 0原文

我有一个正在运行的应用程序,可以通过底部的完整代码拍照。我的问题是如何访问存储在 jpeg 中的缩略图或生成新的缩略图。将代码放在此类中,或者将代码放在一个单独的类中,根据事件或时间表检查文件夹并生成缩略图都可以。

具体来说,我已经设置了缩略图,但无论如何都无法访问它。我尝试过实现 ExifInterface 并尝试手动调整照片大小。我能够将其他参数放入 Jpeg Exif(将一些随机数放入纬度和经度)。查看文件确认数字已被写入。非常感谢。

    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();
    }

这是我使用 Exif 接口尝试但失败的方法:

                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);
    }
}

}

I have an app working that can take a picture via the full code at bottom. My question is regarding how to either access the thumbnail that is stored in the jpeg or generate a new thumbnail. Either putting the code in this class or a separate class that checks the folder based on an event or schedule and generates the thumbnails is fine.

Specifically, I have set for there to be a Thumbnail, but can not access it in anyway. I have tried implementing the ExifInterface and also tried manually resizing the photo. I was able to put other parameters into the Jpeg Exif (put some random numbers in 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 with 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();

Full Code of Class.

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);
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的背包 2024-10-19 09:40:03

我知道这听起来很愚蠢,但我所做的唯一与您不同的事情是使用 File 对象而不是文件名字符串初始化 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);

我似乎记得这让事情对我有用,而以前却不能,尽管我无法想象为什么它会产生任何影响。

I know it sounds silly, but the only thing I'm doing differently than you is initializing the FileOutputStream with a File object instead of a filename string. So where you have

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

try this instead:

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

I seem to remember that this made things work for me where it previously wasn't, although I can't imagine why it would make any difference.

国粹 2024-10-19 09:40:03

而不是

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()));

Instead of

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

try this

FileOutputStream fos = openFileOutput(thumbnail.getPath(), MODE_WORLD_READABLE);
FileOutputStream fos=new FileOutputStream(new File(photo.getPath()));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文