3

我有一个可以通过底部的完整代码拍照的应用程序。我的问题是关于如何访问存储在 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);
    }
}

}

4

2 回答 2

1

我知道这听起来很傻,但我所做的唯一与您不同的是使用 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);

我似乎记得,这让以前无法做到的事情对我有用,尽管我无法想象它为什么会有所作为。

于 2011-12-08T12:04:29.337 回答
0

代替

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()));
于 2012-08-29T09:05:50.660 回答