Image crop and resize in Android

后端 未结 8 965
无人及你
无人及你 2020-12-19 15:58

I followed the instructions below to crop an image.

http://coderzheaven.com/index.php/2011/03/crop-an-image-in-android/

The height and width of the final cro

相关标签:
8条回答
  • 2020-12-19 16:17
    //solution for bit map resizing  
     Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
    
     RectF rectf = new RectF(0, 0, 800, 400);
    
     Canvas canvas = new Canvas(targetBitmap);
     Path path = new Path();
    
     path.addRect(rectf, Path.Direction.CW);
     canvas.clipPath(path);
     int xaxis=bm.getWidth();
     int yaxis=bm.getHeight();
     canvas.drawBitmap( bm, new Rect(0, 0, bm.getWidth(), bm.getHeight()),
                          new Rect(0, 0, targetWidth, targetHeight), paint);
     Matrix matrix = new Matrix();
     matrix.postScale(1f, 1f);
     resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, width, height, matrix, true);
    
     BitmapDrawable bd = new BitmapDrawable(resizedBitmap);
    
    0 讨论(0)
  • 2020-12-19 16:19

    If your code varies from the example, you will have to post it to get precise help. Otherwise, based only on the log - here is an answer:

    In the example you are using, the developer is cropping an image in his app's resources folder (which could be relatively small to start) to a 300x300 bitmap image. First, try cropping a smaller size image in your own app, to a smaller output size, to see if there is a problem with the code, or if you are using an image that is too large for your code or device.

    Since you are saying the final image you are cropping is larger that the screen, that is probably the issue, and you are encountering a basic challenge of using bitmaps in Android. Bitmap file size is a function of pixel width times height - they get bigger fast.

    You can solve by googling other parts of your log, like "bitmap size exceeds VM budget" and you will find several threads on stackoverflow that will help, like this one.

    Strange out of memory issue while loading an image to a Bitmap object

    You may solve it by using bitmap options and inSampleSize, and other techniques.

    0 讨论(0)
  • 2020-12-19 16:20

    You can used crop image using following code that can solve your problem.

    Matrix matrix = new Matrix();
    matrix.postScale(0.5f, 0.5f);
    Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
    

    Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.

    For more detail you can refer this blog

    0 讨论(0)
  • 2020-12-19 16:26

    Look at the similar discussion here, they seem to be having the same byte external allocation too large for this process error.

    0 讨论(0)
  • 2020-12-19 16:29
        //get screen resolution;
        WindowManager display = getWindowManager();
        Point size = new Point();
        display.getDefaultDisplay().getSize(size);      
        int width = size.x;
        int height = size.y;
        // scale the bitmap according to the screen resolution  .
        Bitmap bMap = BitmapFactory.decodeResource(getResources(), .drawable.ima1);
        bMap = Bitmap.createScaledBitmap(bMap, width, height, true);
        // then crop under value of the screen size and bitmap! 
        bMap = Bitmap.createBitmap(bMap, 0, 0, width/2, height);
        Drawable drawa = new BitmapDrawable(getResources(),bMap);
    

    i hope to be usefull

    0 讨论(0)
  • 2020-12-19 16:30

    I further verified the following instructions.

    http://coderzheaven.com/index.php/2011/03/crop-an-image-in-android/

    I found that the codes are not good enough and some of them are redundant.

    To crop a big bitmap image, there is no need to use matrix to do the task.

    Just use canvas.drawBitmap method can crop the big image !

    Also no need to touch the BitmapFactory.Options.

    Now I simply crop the image and let the imageview resize it. No more out-of-memory error. Bingo !

    0 讨论(0)
提交回复
热议问题