开发者

changeing image file resoultion in android

i am saving a captured image in sdcard by using following code

class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo=new File(Environment.getExternalStorageDirectory(),"photo.jpg");
if (photo.exists()) {
photo.delete();
}

try {
FileOutputStream fos=new FileOutputStream(photo.getPath());     
fos.write(jpeg[0]);
fos.close();
}
catch (java.io.IOException e) {
Log.e("PictureDemo", "Exception in photoCallback", e);
}
return(null);
}
}

but i am getting the image of resolution 1024*768 how can i change the resoultion of that image.

i am calling SavePhotoTask like this

Camera.PictureCallback photoCallback=new Camera.PictureCallback(){
publ开发者_Go百科ic void onPictureTaken(byte[] data, Camera camera){

bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);

Canvas canvas = new Canvas(mutableBitmap);
canvas.drawBitmap(itembmp,left,right,null);
image.setImageBitmap(mutableBitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
mutableBitmap.compress(Bitmap.CompressFormat.PNG,100, stream);
byte[] byteArray = stream.toByteArray();
new SavePhotoTask().execute(byteArray);
Toast.makeText(PreviewDemo1.this,"Image Saved",Toast.LENGTH_LONG).show();
camera.startPreview();
inPreview=true;
}
};

thanks in advance


It is the jpeg passed to the doInBackground method that already has that resolution - you need to change whatever is calling this code.


If you can parse it to BitMap then you can use this:

private final int MAX_WIDTH = 400;
private final int MAX_HEIGHT = 400;
public Bitmap getResizedBitmap(Bitmap bm) {
        int width = bm.getWidth();
        int height = bm.getHeight();
        float scaleWidth;
        float scaleHeight;
        if (width < MAX_WIDTH && height < MAX_HEIGHT) {
            return bm;
        }
        if (width > height) {
            scaleWidth = ((float) MAX_WIDTH) / width;
            scaleHeight = ((float) MAX_HEIGHT * height / width) / height;

        } else {
            scaleWidth = ((float) MAX_WIDTH * width / height) / width;
            scaleHeight = ((float) MAX_HEIGHT) / height;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
                matrix, false);

        return resizedBitmap;

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜