Search in sources :

Example 1 with RequiresPermission

use of android.support.annotation.RequiresPermission in project android-vision by googlesamples.

the class CameraSource method start.

/**
     * Opens the camera and starts sending preview frames to the underlying detector.  The preview
     * frames are not displayed.
     *
     * @throws IOException if the camera's preview texture or display could not be initialized
     */
@RequiresPermission(Manifest.permission.CAMERA)
public CameraSource start() throws IOException {
    synchronized (mCameraLock) {
        if (mCamera != null) {
            return this;
        }
        mCamera = createCamera();
        // old version of Android. fall back to use SurfaceView.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mDummySurfaceTexture = new SurfaceTexture(DUMMY_TEXTURE_NAME);
            mCamera.setPreviewTexture(mDummySurfaceTexture);
        } else {
            mDummySurfaceView = new SurfaceView(mContext);
            mCamera.setPreviewDisplay(mDummySurfaceView.getHolder());
        }
        mCamera.startPreview();
        mProcessingThread = new Thread(mFrameProcessor);
        mFrameProcessor.setActive(true);
        mProcessingThread.start();
    }
    return this;
}
Also used : SurfaceTexture(android.graphics.SurfaceTexture) SurfaceView(android.view.SurfaceView) RequiresPermission(android.support.annotation.RequiresPermission)

Example 2 with RequiresPermission

use of android.support.annotation.RequiresPermission in project android-vision by googlesamples.

the class CameraSource method start.

/**
     * Opens the camera and starts sending preview frames to the underlying detector.  The preview
     * frames are not displayed.
     *
     * @throws IOException if the camera's preview texture or display could not be initialized
     */
@RequiresPermission(Manifest.permission.CAMERA)
public CameraSource start() throws IOException {
    synchronized (mCameraLock) {
        if (mCamera != null) {
            return this;
        }
        mCamera = createCamera();
        // old version of Android. fall back to use SurfaceView.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mDummySurfaceTexture = new SurfaceTexture(DUMMY_TEXTURE_NAME);
            mCamera.setPreviewTexture(mDummySurfaceTexture);
        } else {
            mDummySurfaceView = new SurfaceView(mContext);
            mCamera.setPreviewDisplay(mDummySurfaceView.getHolder());
        }
        mCamera.startPreview();
        mProcessingThread = new Thread(mFrameProcessor);
        mFrameProcessor.setActive(true);
        mProcessingThread.start();
    }
    return this;
}
Also used : SurfaceTexture(android.graphics.SurfaceTexture) SurfaceView(android.view.SurfaceView) RequiresPermission(android.support.annotation.RequiresPermission)

Example 3 with RequiresPermission

use of android.support.annotation.RequiresPermission in project android-vision by googlesamples.

the class CameraSourcePreview method startIfReady.

@RequiresPermission(Manifest.permission.CAMERA)
private void startIfReady() throws IOException, SecurityException {
    if (mStartRequested && mSurfaceAvailable) {
        mCameraSource.start(mSurfaceView.getHolder());
        if (mOverlay != null) {
            Size size = mCameraSource.getPreviewSize();
            int min = Math.min(size.getWidth(), size.getHeight());
            int max = Math.max(size.getWidth(), size.getHeight());
            if (isPortraitMode()) {
                // Swap width and height sizes when in portrait, since it will be rotated by
                // 90 degrees
                mOverlay.setCameraInfo(min, max, mCameraSource.getCameraFacing());
            } else {
                mOverlay.setCameraInfo(max, min, mCameraSource.getCameraFacing());
            }
            mOverlay.clear();
        }
        mStartRequested = false;
    }
}
Also used : Size(com.google.android.gms.common.images.Size) RequiresPermission(android.support.annotation.RequiresPermission)

Example 4 with RequiresPermission

use of android.support.annotation.RequiresPermission in project Talon-for-Twitter by klinker24.

the class PhotoViewerActivity method downloadImage.

@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
private void downloadImage() {
    new Thread(new Runnable() {

        @Override
        public void run() {
            Looper.prepare();
            try {
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_stat_icon).setTicker(getResources().getString(R.string.downloading) + "...").setContentTitle(getResources().getString(R.string.app_name)).setContentText(getResources().getString(R.string.saving_picture) + "...").setProgress(100, 100, true).setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_save));
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(6, mBuilder.build());
                URL mUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
                InputStream is = new BufferedInputStream(conn.getInputStream());
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = false;
                Bitmap bitmap = decodeSampledBitmapFromResourceMemOpt(is, 600, 600);
                Random generator = new Random();
                int n = 1000000;
                n = generator.nextInt(n);
                String fname = "Image-" + n;
                Uri uri = IOUtils.saveImage(bitmap, fname, context);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                intent.setDataAndType(uri, "image/*");
                PendingIntent pending = PendingIntent.getActivity(context, 91, intent, 0);
                mBuilder = new NotificationCompat.Builder(context).setContentIntent(pending).setSmallIcon(R.drawable.ic_stat_icon).setTicker(getResources().getString(R.string.saved_picture) + "...").setContentTitle(getResources().getString(R.string.app_name)).setContentText(getResources().getString(R.string.saved_picture) + "!").setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_save));
                mNotificationManager.notify(6, mBuilder.build());
            } catch (Exception e) {
                Log.e(LOGGER_TAG, "Exception while saving photo", e);
                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(R.drawable.ic_stat_icon).setTicker(getResources().getString(R.string.error) + "...").setContentTitle(getResources().getString(R.string.app_name)).setContentText(getResources().getString(R.string.error) + "...").setProgress(100, 100, true).setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_action_save));
                NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                mNotificationManager.notify(6, mBuilder.build());
            }
        }
    }).start();
    finish();
}
Also used : NotificationManager(android.app.NotificationManager) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) Uri(android.net.Uri) URL(java.net.URL) Bitmap(android.graphics.Bitmap) HttpURLConnection(java.net.HttpURLConnection) Random(java.util.Random) BufferedInputStream(java.io.BufferedInputStream) NotificationCompat(android.support.v4.app.NotificationCompat) BitmapFactory(android.graphics.BitmapFactory) PendingIntent(android.app.PendingIntent) RequiresPermission(android.support.annotation.RequiresPermission)

Example 5 with RequiresPermission

use of android.support.annotation.RequiresPermission in project Talon-for-Twitter by klinker24.

the class IOUtils method saveImage.

@RequiresPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
public static Uri saveImage(Bitmap finalBitmap, String d, Context context) throws IOException {
    File talonDir = getPicturesDirectory();
    String fname = d + ".jpg";
    File file = new File(talonDir, fname);
    if (file.exists())
        file.delete();
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.close();
    } catch (IOException e) {
        Log.e(LOGGER_TAG, "Error while saving image", e);
        throw e;
    }
    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
    values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Toast.makeText(context, context.getResources().getString(R.string.save_image), Toast.LENGTH_SHORT).show();
    return Uri.fromFile(file);
}
Also used : ContentValues(android.content.ContentValues) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) RequiresPermission(android.support.annotation.RequiresPermission)

Aggregations

RequiresPermission (android.support.annotation.RequiresPermission)7 SurfaceTexture (android.graphics.SurfaceTexture)2 SurfaceView (android.view.SurfaceView)2 Size (com.google.android.gms.common.images.Size)2 ActivityManager (android.app.ActivityManager)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 ContentValues (android.content.ContentValues)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 BitmapFactory (android.graphics.BitmapFactory)1 Uri (android.net.Uri)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1