Cropping Video In Android

crop_video

Hey guyz, First of all to cropping video we need to use TextureView and this component available from android API 14.

Step 1: Create activity/fragment layout

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextureView
        android:id="@+id/textureView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Step 2: Add video file

If you want to add video to your project you should add it to /res/raw directory

Step 3: Add below code to activity/fragment

    ...

    public static final int MY_VIDEO_WIDTH = 1920;
    public static final int MY_VIDEO_HEIGHT = 1080;

    // I'm using ButterKnife binding my views you can use findViewById instead
    @BindView(R.id.textureView)
    TextureView textureView;
    MediaPlayer mediaPlayer;

    ...

    @Override
    public void onStart() {
        super.onStart();
        textureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
        Surface surface = new Surface(surfaceTexture);
        scaleToCropCenter(MY_VIDEO_WIDTH, MY_VIDEO_HEIGHT, this.textureView.getWidth(), this.textureView.getHeight());

        try {
            mediaPlayer = MediaPlayer.create(getActivity(), R.raw.video);
            mediaPlayer.setSurface(surface);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void scaleToCropCenter(int videoWidth, int videoHeight, int viewWidth, int viewHeight) {
        float scaleX = 1.0f;
        float scaleY = 1.0f;

        if (videoWidth > viewWidth && videoHeight > viewHeight) {
            scaleX = division(videoWidth, viewWidth);
            scaleY = division(videoHeight, viewHeight);
        } else if (videoWidth < viewWidth && videoHeight < viewHeight) {             scaleY = division(viewWidth , videoWidth);             scaleX = division(viewHeight , videoHeight);         } else if (viewWidth > videoWidth) {
            scaleY = division(division(viewWidth, videoWidth), division(viewHeight, videoHeight));
        } else if (viewHeight > videoHeight) {
            scaleX = division(division(videoWidth, viewWidth), division(viewHeight, videoHeight));
        }

        int pivotPointX = viewWidth / 2;
        int pivotPointY = viewHeight / 2;

        Matrix matrix = new Matrix();
        matrix.setScale(scaleX, scaleY, pivotPointX, pivotPointY);

        textureView.setTransform(matrix);
        textureView.setLayoutParams(new FrameLayout.LayoutParams(viewWidth, viewHeight));
    }

    private float division(float value1, float value2) {
        if (value1 > value2)
            return value1 / value2;
        else return value2 / value1;
    }

    ...

That’s it ūüėČ

Advertisements

Simple Practices for Designing RESTful API

RESTful-Area

Following these standards means:

  • Documentation will make more sense and so developers will understand your API quicker.
  • Developers will therefore be able to integrate quicker and therefore deliver products faster and at lower cost.
  • Many of the principles will make your clients faster,¬†improving product quality

1. Use nouns but not verb

For an easy understanding use this structure for every resources:
Screenshot from 2016-04-06 01:11:33

Note: Do not mix up singular and plural nouns, Keep it simple and use only plural nouns for all resources

2. Use sub-resources for relations

If a resource is related to another resource use subresources
Example: GET /cars/711/drivers/4  Return driver #4 for car 711

3. Provide filtering, sorting and paging for collections

Filtering:

Use a unique query parameter for all fields.
Example: GET /cars?color=red

Sorting:

Allow ascending and descending sorting over multiple fields.
Example: GET /cars?sort=-model

Pagination

Use limit and next URL. It is easy for the client to showing items.
Example: /cars?limit=10

Response:
{
items:[…],
nextURL:”…”
}

4. Version your API

Make the API Version mandatory and do not release an unversioned API.
Note: Use a simple ordinal number and avoid dot notation such as 2.5

5. Error handling

Error responses should include a common HTTP status code, message for developer, message for the end-user (when appropriate).
For example:

{
“status”:400,
“developerMessage”:”Verbose, plain language description of the problem”,
“userMessage”:”This is a message that can be passed along to end-users, if needed.”
}

Use three simple, common response codes indicating (1) success, (2) failure due to client-side problem, (3) failure due to server-side problem:

  1. 200 – OK
  2. 400 – Bad Request
  3. 500 – Internal Server Error

6. Using actions

Sometimes, it is required to expose an operation in the API that inherently is non RESTful. Actions are basically RPC-like messages to a resource to perform a certain operation.

Example: POST /users/12/follow   Following user #12

Note: The actions should only be used as an exception, when there is a good reason that an operation cannot be mapped to one of the standard RESTful methods.

Thank you guys:

https://github.com/WhiteHouse/api-standards
http://mark-kirby.co.uk/2013/creating-a-true-rest-api/#comments
http://restful-api-design.readthedocs.org/en/latest/methods.html
http://blog.mwaysolutions.com/2014/06/05/10-best-practices-for-better-restful-api/