Created
February 4, 2019 09:54
-
-
Save luQman704/62408c7b332b9240d23d0d22aa53a294 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.database.Cursor; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Color; | |
import android.graphics.Matrix; | |
import android.hardware.Camera; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.SurfaceHolder; | |
import android.view.SurfaceView; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.Button; | |
import android.widget.FrameLayout; | |
import android.widget.RelativeLayout; | |
import android.widget.TabHost; | |
import android.widget.Toast; | |
import com.google.android.material.tabs.TabLayout; | |
import com.sothree.slidinguppanel.ScrollableViewHelper; | |
import com.sothree.slidinguppanel.SlidingUpPanelLayout; | |
import com.theartofdev.edmodo.cropper.CropImageView; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.io.OutputStream; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.Date; | |
import java.util.Objects; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.core.widget.NestedScrollView; | |
import androidx.fragment.app.Fragment; | |
import androidx.lifecycle.Observer; | |
import androidx.lifecycle.ViewModelProviders; | |
import androidx.recyclerview.widget.DefaultItemAnimator; | |
import androidx.recyclerview.widget.GridLayoutManager; | |
import androidx.recyclerview.widget.LinearLayoutManager; | |
import androidx.recyclerview.widget.RecyclerView; | |
public class camera extends Fragment { | |
private Camera mCamera; | |
View layout3; | |
Button flashLight; | |
Button flashLightOff; | |
Boolean flashIsOn= false; | |
public static final int MEDIA_TYPE_IMAGE = 1; | |
public static final int MEDIA_TYPE_VIDEO = 2; | |
RelativeLayout preview; | |
static File mediaFile; | |
private SharedViewModel model; | |
String path; | |
FrameLayout frame; | |
CropImageView cropImageView; | |
Button crop; | |
public CameraPreview mPreview; | |
TabLayout tabLayout; | |
private RecyclerView my_recycler_view; | |
private RecyclerView my_recycler_view_up; | |
private RecyclerView.Adapter my_adapter; | |
private RecyclerView.LayoutManager my_layout_manager; | |
private RecyclerView.LayoutManager my_layout_manager_up; | |
ArrayList<String> list = new ArrayList<String>(); | |
private SlidingUpPanelLayout slidingLayout; | |
Button capture; | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState){ | |
//super.onCreate(savedInstanceState); | |
layout3 = inflater.inflate(R.layout.camerafragment,container,false); | |
mCamera=getCameraInstance(); | |
preview= (RelativeLayout) layout3.findViewById(R.id.camera_preview); | |
startTrasanction(); | |
capture = (Button) layout3.findViewById(R.id.btnCamera); | |
frame =(FrameLayout) layout3.findViewById(R.id.frame); | |
cropImageView=(CropImageView) layout3.findViewById(R.id.cropImageView); | |
crop = (Button) layout3.findViewById(R.id.btnCrop); | |
crop.setVisibility(View.GONE); | |
cropImageView.setVisibility(View.GONE); | |
model = ViewModelProviders.of(getActivity()).get(SharedViewModel.class); | |
my_recycler_view= (RecyclerView) layout3.findViewById(R.id.my_recycler_view); | |
my_recycler_view_up= (RecyclerView) layout3.findViewById(R.id.my_recycler_view_up); | |
my_layout_manager= new LinearLayoutManager(getActivity()); | |
my_layout_manager_up= new GridLayoutManager(getActivity(),3); | |
((LinearLayoutManager) my_layout_manager).setOrientation(LinearLayoutManager.HORIZONTAL); | |
my_recycler_view.setLayoutManager(my_layout_manager); | |
my_recycler_view_up.setLayoutManager(my_layout_manager_up); | |
slidingLayout = (SlidingUpPanelLayout) layout3.findViewById(R.id.sliding_layout); | |
//some "demo" event | |
slidingLayout.addPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() { | |
@Override | |
public void onPanelSlide(View panel, float slideOffset) { | |
Log.d("Files", "onPanelSlide, offset " + slideOffset); | |
} | |
@Override | |
public void onPanelStateChanged(View panel, SlidingUpPanelLayout.PanelState previousState, SlidingUpPanelLayout.PanelState newState) { | |
Log.d("Files", "onPanelSlide, offset " + newState + "previous state"+ previousState); | |
if(newState.equals(SlidingUpPanelLayout.PanelState.EXPANDED)){ | |
my_recycler_view.setVisibility(View.GONE); | |
my_recycler_view_up.setVisibility(View.VISIBLE); | |
}else{ | |
my_recycler_view_up.setVisibility(View.GONE); | |
my_recycler_view.setVisibility(View.VISIBLE); | |
} | |
} | |
}); | |
try { | |
final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}; | |
final String orderBy = MediaStore.Images.Media._ID; | |
//Stores all the images from the gallery in Cursor | |
Cursor cursor = getActivity().getContentResolver().query( | |
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, | |
null, orderBy); | |
//Total number of images | |
int count = cursor.getCount(); | |
//Create an array to store path to all the images | |
String[] arrPath = new String[count]; | |
for (int i = 0; i < count; i++) { | |
cursor.moveToPosition(i); | |
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); | |
//Store the path of the image | |
arrPath[i] = cursor.getString(dataColumnIndex); | |
list.add(arrPath[i]); | |
Log.d("Files", arrPath[i]); | |
} | |
// The cursor should be freed up after use with close() | |
cursor.close(); | |
my_adapter= new MyAdapter(list,getActivity()); | |
my_recycler_view.setItemAnimator(new DefaultItemAnimator()); | |
my_recycler_view_up.setItemAnimator(new DefaultItemAnimator()); | |
my_recycler_view.setAdapter(my_adapter); | |
my_recycler_view_up.setAdapter(my_adapter); | |
}catch(Exception e){ | |
Log.d("Files", e.getMessage()); | |
} | |
capture.setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// get an image from the camera | |
mCamera.takePicture(null, null, mPicture); | |
//releaseCamera(); | |
//preview.removeAllViews(); | |
} | |
} | |
); | |
flashLight= (Button) layout3.findViewById(R.id.btnFlash); | |
flashLightOff=(Button) layout3.findViewById(R.id.btnFlashIsOn); | |
flashLightOff.setVisibility(View.GONE); | |
flashLightOff.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(flashIsOn){ | |
Camera.Parameters parameters = mCamera.getParameters(); | |
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); | |
mCamera.setParameters(parameters); | |
flashLight.setVisibility(View.VISIBLE); | |
flashLightOff.setVisibility(View.GONE); | |
flashIsOn = false; | |
} | |
} | |
}); | |
flashLight.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if(!flashIsOn) { | |
if (mCamera != null) { | |
Camera.Parameters parameters = mCamera.getParameters(); | |
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); | |
mCamera.setParameters(parameters); | |
flashLight.setVisibility(View.GONE); | |
flashLightOff.setVisibility(View.VISIBLE); | |
flashIsOn = true; | |
} | |
} | |
} | |
}); | |
model.getImageName().observe(this, new Observer<String>() { | |
@Override | |
public void onChanged(String s) { | |
Log.d("Files","here is what you need" + " "+ s); | |
try{ | |
slidingLayout.setPanelState(SlidingUpPanelLayout.PanelState.COLLAPSED); | |
mCamera.stopPreview(); | |
mCamera.release(); | |
mCamera=null; | |
preview.removeView(mPreview); | |
crop.setVisibility(View.VISIBLE); | |
flashLight.setVisibility(View.GONE); | |
capture.setVisibility(View.GONE); | |
}catch (Exception d){ | |
Log.d("Files",d.getMessage()); | |
} | |
showCropper(s); | |
} | |
}); | |
return layout3; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment