a
554325746@qq.com
2019-12-25 603cb36a5123e46656b06a5deb8d7ac7ff81307f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package com.basic.security.utils;
 
import android.graphics.Bitmap;
import android.util.Log;
 
import java.nio.ByteBuffer;
 
public class BitmapHolder {
    static {
        //System.loadLibrary("JniBitmapOperationsLibrary");
    }
 
    ByteBuffer _handler = null;
 
    public BitmapHolder() {
    }
 
    public BitmapHolder(final Bitmap bitmap) {
        storeBitmap(bitmap);
    }
 
    private native ByteBuffer jniStoreBitmapData(Bitmap bitmap);
 
    private native Bitmap jniGetBitmapFromStoredBitmapData(ByteBuffer handler);
 
    private native void jniFreeBitmapData(ByteBuffer handler);
 
    private native void jniRotateBitmapCcw90(ByteBuffer handler);
 
    private native void jniRotateBitmapCw90(ByteBuffer handler);
 
    private native void jniRotateBitmap180(ByteBuffer handler);
 
    private native void jniCropBitmap(ByteBuffer handler, final int left, final int top, final int right, final int bottom);
 
    private native void jniScaleNNBitmap(ByteBuffer handler, final int newWidth, final int newHeight);
 
    private native void jniScaleBIBitmap(ByteBuffer handler, final int newWidth, final int newHeight);
 
    private native void jniFlipBitmapHorizontal(ByteBuffer handler);
 
    private native void jniFlipBitmapVertical(ByteBuffer handler);
 
    public void storeBitmap(final Bitmap bitmap) {
        if (_handler != null)
            freeBitmap();
        _handler = jniStoreBitmapData(bitmap);
    }
 
    public void rotateBitmapCcw90() {
        if (_handler == null)
            return;
        jniRotateBitmapCcw90(_handler);
    }
 
    public void rotateBitmapCw90() {
        if (_handler == null)
            return;
        jniRotateBitmapCw90(_handler);
    }
 
    public void rotateBitmap180() {
        if (_handler == null)
            return;
        jniRotateBitmap180(_handler);
    }
 
    public void cropBitmap(final int left, final int top, final int right, final int bottom) {
        if (_handler == null)
            return;
        jniCropBitmap(_handler, left, top, right, bottom);
    }
 
    public Bitmap getBitmap() {
        if (_handler == null)
            return null;
        return jniGetBitmapFromStoredBitmapData(_handler);
    }
 
    public Bitmap getBitmapAndFree() {
        final Bitmap bitmap = getBitmap();
        freeBitmap();
        return bitmap;
    }
 
    public void scaleBitmap(final int newWidth, final int newHeight, final ScaleMethod scaleMethod) {
        if (_handler == null)
            return;
        switch (scaleMethod) {
            case BilinearInterpolation:
                jniScaleBIBitmap(_handler, newWidth, newHeight);
                break;
            case NearestNeighbour:
                jniScaleNNBitmap(_handler, newWidth, newHeight);
                break;
        }
    }
 
    /**
     * flips a bitmap horizontally, as such: <br/>
     * <p>
     * <pre>
     * 123    321
     * 456 => 654
     * 789    987
     * </pre>
     */
    //
    public void flipBitmapHorizontal() {
        if (_handler == null)
            return;
        jniFlipBitmapHorizontal(_handler);
    }
 
    /**
     * Flips the bitmap on the vertically, as such:<br/>
     * <p>
     * <pre>
     * 123    789
     * 456 => 456
     * 789    123
     * </pre>
     */
    public void flipBitmapVertical() {
        if (_handler == null)
            return;
        jniFlipBitmapVertical(_handler);
    }
 
    public void freeBitmap() {
        if (_handler == null)
            return;
        jniFreeBitmapData(_handler);
        _handler = null;
    }
 
    protected void finalize() throws Throwable {
        super.finalize();
        if (_handler == null)
            return;
        Log.w("DEBUG", "JNI bitmap wasn't freed nicely.please remember to free the bitmap as soon as you can");
        freeBitmap();
    }
 
    public enum ScaleMethod {
        NearestNeighbour, BilinearInterpolation
    }
}