protected
ArrayList<Model> initModelsConfig() {
ArrayList<Model> models =
new
ArrayList<>();
for
(
int
i =
0
; i < modelNameList.length; i++) {
Model model =
new
Model();
model.type = i;
model.iconRes = resIds[i];
model.modelName = modelNameList[i];
models.add(model);
}
return
models;
}
imageIntValues =
new
int
[INPUT_IMAGE_SIZE_WIDTH * INPUT_IMAGE_SIZE_HEIGHT];
imageFloatValues =
new
float
[INPUT_IMAGE_SIZE_WIDTH * INPUT_IMAGE_SIZE_HEIGHT *
3
];
ArrayList<Model> modelsConfig = initModelsConfig();
StyleButtonAdapter styleButtonAdapter =
new
StyleButtonAdapter(CameraActivity.
this
, modelsConfig);
recyclerView.setAdapter(styleButtonAdapter);
cameraKitView.onStart();
facingButton.setOnTouchListener(facingButtonTouchListener);
flashButton.setOnTouchListener(flashButtonTouchListener);
captureButton.setOnTouchListener(captureButtonTouchListener);
styleSplitorLayout.setOnTouchListener(styleSplitorTouchListener);
styleSplitButton.setOnTouchListener(styleSpiltButtonTouchListener);
refreshButton.setOnTouchListener(refreshButtonTouchListener);
styleButtonAdapter.buttonSetOnclick(
new
StyleButtonAdapter.ButtonInterface() {
@Override
public
void
onclick(View view, Model model) {
Toast toast = Toast.makeText(CameraActivity.
this
,
"이 필터 골랐네요:"
+ model.modelName, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM,
0
,
350
);
toast.show();
selectedModel = model.modelName;
}
});
}
private
void
cropPictureLeft() {
float
x = styleSplitorLayout.getX() + styleSplitor.getX();
float
rate = x / imageOverlayLayout.getWidth();
int
imageWidth = captureImageBitMap.getWidth();
int
imageHeight = captureImageBitMap.getHeight();
int
width = (
int
) (imageWidth * rate);
if
(width <=
0
) {
pictureLeft.setLayoutParams(
new
RelativeLayout.LayoutParams(
0
, pictureLeft.getHeight()));
}
else
{
Bitmap bitmap = Bitmap.createBitmap(captureImageBitMap,
0
,
0
, width, imageHeight);
pictureLeft.setLayoutParams(
new
RelativeLayout.LayoutParams((
int
) x, pictureLeft.getHeight()));
pictureLeft.setImageBitmap(bitmap);
}
}
private
View.OnTouchListener styleSplitorTouchListener =
new
View.OnTouchListener() {
@Override
public
boolean
onTouch(View view, MotionEvent motionEvent) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
switch
(motionEvent.getAction()) {
case
MotionEvent.ACTION_MOVE:
params.leftMargin = params.leftMargin + (
int
) motionEvent.getX();
view.setLayoutParams(params);
cropPictureLeft();
break
;
case
MotionEvent.ACTION_UP:
params.leftMargin = params.leftMargin + (
int
) motionEvent.getX();
view.setLayoutParams(params);
break
;
}
return
true
;
}
};
void
changeViewImageResource(
final
ImageView imageView,
@DrawableRes
final
int
resId) {
imageView.setRotation(
0
);
imageView.animate()
.rotationBy(
360
)
.setDuration(
400
)
.setInterpolator(
new
OvershootInterpolator())
.start();
imageView.postDelayed(
new
Runnable() {
@Override
public
void
run() {
imageView.setImageResource(resId);
}
},
120
);
}
boolean
handleViewTouchFeedback(View view, MotionEvent motionEvent) {
switch
(motionEvent.getAction()) {
case
MotionEvent.ACTION_DOWN: {
touchDownAnimation(view);
return
true
;
}
case
MotionEvent.ACTION_UP: {
touchUpAnimation(view);
return
true
;
}
default
: {
return
true
;
}
}
}
int
inputWidth = input.getWidth();
int
inputHeight = input.getHeight();
Matrix matrix =
new
Matrix();
matrix.postScale(((
float
) width) / inputWidth, ((
float
) height) / inputHeight);
Bitmap scaledBitmap = Bitmap.createBitmap(input,
0
,
0
, inputWidth, inputHeight, matrix,
false
);
return
scaledBitmap;
}
private
Bitmap imageStyleTransfer(Bitmap bitmap) {
Bitmap scaledBitmap = scaleBitmap(bitmap, INPUT_IMAGE_SIZE_WIDTH, INPUT_IMAGE_SIZE_HEIGHT);
scaledBitmap.getPixels(imageIntValues,
0
, scaledBitmap.getWidth(),
0
,
0
, scaledBitmap.getWidth(), scaledBitmap.getHeight());
for
(
int
i =
0
; i < imageIntValues.length; ++i) {
final
int
val = imageIntValues[i];
imageFloatValues[i *
3
+
0
] = ((val >>
16
) &
0xFF
) *
1
.0f;
imageFloatValues[i *
3
+
1
] = ((val >>
8
) &
0xFF
) *
1
.0f;
imageFloatValues[i *
3
+
2
] = (val &
0xFF
) *
1
.0f;
}
Trace.beginSection(
"feed"
);
tensorFlowInferenceInterface.feed(INPUT_NAME, imageFloatValues, INPUT_IMAGE_SIZE_WIDTH, INPUT_IMAGE_SIZE_HEIGHT,
3
);
Trace.endSection();
Trace.beginSection(
"run"
);
tensorFlowInferenceInterface.run(
new
String[]{OUTPUT_NAME});
Trace.endSection();
Trace.beginSection(
"fetch"
);
tensorFlowInferenceInterface.fetch(OUTPUT_NAME, imageFloatValues);
Trace.endSection();
for
(
int
i =
0
; i < imageIntValues.length; ++i) {
imageIntValues[i] =
0xFF000000
| (((
int
) (imageFloatValues[i *
3
+
0
])) <<
16
)
| (((
int
) (imageFloatValues[i *
3
+
1
])) <<
8
)
| ((
int
) (imageFloatValues[i *
3
+
2
]));
}
scaledBitmap.setPixels(imageIntValues,
0
, scaledBitmap.getWidth(),
0
,
0
, scaledBitmap.getWidth(), scaledBitmap.getHeight());
return
scaledBitmap;
}
}